Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. <?php
  2. $db_host = "localhost";
  3. $db_user = "mike";
  4. $db_pass = "test";
  5. $db_name = "bogdan";
  6. // after import, test with cat_id=61
  7. $cat_id = (int)$_REQUEST['cat_id'];
  8. if (!$cat_id) die("No category id specified");
  9.  
  10. $db_link = mysql_connect($db_host, $db_user, $db_pass);
  11. if ($db_link === false) die("Could not connect to database\n");
  12. $sel_db = mysql_select_db($db_name, $db_link);
  13. if ($sel_db === false) die("Could not select db $db_name\n");
  14.  
  15. // get main category
  16. $sql = "SELECT * FROM `categories` WHERE `id` = '".$cat_id."'";
  17. $res = mysql_query($sql);
  18. $data = mysql_fetch_assoc($res);
  19. if ((int)$data['id']) $data['subcats'] = getSubCategories($data['id']);
  20.  
  21. function getSubCategories($id) {
  22.     if (!(int)$id) return false;
  23.     $result = array();
  24.    
  25.     $sql = "SELECT * FROM `categories` WHERE `parent_id` = '".$id."'";
  26.     $res = mysql_query($sql);
  27.     if ($res !== false) {
  28.         while ($row = mysql_fetch_assoc($res)) {
  29.             $row['subcats'] = getSubCategories($row['id']); // recursive call
  30.             $result[] = $row;
  31.         }
  32.     }
  33.     return $result;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement