Jump to content

Help With a Category Table


drumminlogan

Recommended Posts

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

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.