drumminlogan Posted May 23, 2007 Share Posted May 23, 2007 I am brand new to php and am building my first full script and could use some help. I am building a basic content management system that will have different categories and subcategories. I want on each of the categories to print a path for example: Home: Business: Franchises Here is how i have my categories table setup. CategoryId CategoryName CategoryParent 1Business0 2Sports0 3Franchises1 If the category is a subcategory, it has a parent category, if not, its value is set to 0. i know how to print the category name, but am not sure how to print the parent category/plan for another level of categories. here is my code so far. Any help would be appreciated. Thanks. <?php require_once ('connection.php'); $query = "SELECT * FROM categories WHERE catid = $type"; if ($r = mysql_query ($query)) { while ($row = mysql_fetch_array ($r)) { print " <div class=\"path\"><a href=\"../index.php\">Home</a>: <a href=\"index.php\">$</a>: {$row['catname']}</div>"; } } else { die ('<p>Could not retrieve the data because: <strong>' . mysql_error() . "</strong>. The query was $query.</p>"); }?> Link to comment https://forums.phpfreaks.com/topic/52600-help-with-a-category-table/ Share on other sites More sharing options...
drumminlogan Posted May 25, 2007 Author Share Posted May 25, 2007 Any ideas or resources i could look to that might help me? thanks. Link to comment https://forums.phpfreaks.com/topic/52600-help-with-a-category-table/#findComment-261368 Share on other sites More sharing options...
sasa Posted May 25, 2007 Share Posted May 25, 2007 try <?php function my_path($a) { if ($a == 0) return ''; $query = "SELECT * FROM categories WHERE catid = $a"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); mysql_free_result($result); $x = my_path($row['CategoryParent'],'/'); $x = $x ? $x.'/' : $x; return $x.$row['catname']; } mysql_connect(); mysql_select_db('test'); echo my_path(3); ?> Link to comment https://forums.phpfreaks.com/topic/52600-help-with-a-category-table/#findComment-261392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.