Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.