Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $db_host = "localhost";
- $db_user = "mike";
- $db_pass = "test";
- $db_name = "bogdan";
- // after import, test with cat_id=61
- $cat_id = (int)$_REQUEST['cat_id'];
- if (!$cat_id) die("No category id specified");
- $db_link = mysql_connect($db_host, $db_user, $db_pass);
- if ($db_link === false) die("Could not connect to database\n");
- $sel_db = mysql_select_db($db_name, $db_link);
- if ($sel_db === false) die("Could not select db $db_name\n");
- // get main category
- $sql = "SELECT * FROM `categories` WHERE `id` = '".$cat_id."'";
- $res = mysql_query($sql);
- $data = mysql_fetch_assoc($res);
- if ((int)$data['id']) $data['subcats'] = getSubCategories($data['id']);
- function getSubCategories($id) {
- if (!(int)$id) return false;
- $result = array();
- $sql = "SELECT * FROM `categories` WHERE `parent_id` = '".$id."'";
- $res = mysql_query($sql);
- if ($res !== false) {
- while ($row = mysql_fetch_assoc($res)) {
- $row['subcats'] = getSubCategories($row['id']); // recursive call
- $result[] = $row;
- }
- }
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement