View Full Version : Trouble with VBulletin like Hierarchy
Chousho
Sun 24th Sep '06, 8:04am
I'm trying to do something like VB does, where on a thread the areas above are listed.
For example
VBulletin Community Forum > General > PHP & HTML Questions.
I'm trying to do something like that. I have it set up so that it would be something like (in the db)
Pid = 1, parentid = 0, title = VBulletin Community Forum
Pid = 2, parentid = 1, title = News and Info
Pid = 3, parentid = 1, title = General
Pid = 4, parentid = 3, title = Database Help
Pid = 5, parentid = 3, title = PHP & HTML Questions
So for instance, I have a page set up where it gets $id and grabs the Pid with that number. Along with this, I want it to grab the parentid, print that title, grab that parent's parentid, print the title, and keep doing so until parentid = 0.
So far I have this, but it doesn't work, and I'm getting frustrated.
$oneup = @mysql_query("SELECT parentid FROM page WHERE pageid = $id");
while($row = mysql_fetch_array($oneup)) {//loop through each returned row
if ($row['parentid'] ==0){
$sql = @mysql_query("SELECT title FROM page WHERE pageid = $oneup");
$data = mysql_fetch_array($sql);
$title = $data['title'];
}
}
echo ": $title"; Anybody see something I'm not?
nico_swd
Sun 24th Sep '06, 3:19pm
$sql = @mysql_query("SELECT title FROM page WHERE pageid = $oneup");
$oneup is a resource, and cannot be used like that. Try $row['pid'] instead.
Also note that $title is inside the loop and will be overwritten each time. So at the end it will only hold the last value from the loop. You can add a dot infront of the equal sign, so it will add each title from the loop to the current string.
$title .= $data['title'];
I'm not sure if you're over-complicating this. I think it would be easier if you had a single table for the categories, such as General, and a single table for the pages. Then you can place each page easily into a category assigning it to a number which is the category ID.
Here an example how I'd do it.
$query = mysql_query("SELECT * FROM page WHERE pageid = ". $id);
$page = mysql_fetch_array($query);
/**
Now $page hold all data for this page such as the categoryid, title, content, etc... So let's go ahead and grab the category title
*/
$categories = mysql_query("SELECT * FROM categories WHERE categoryid = ". $page['categoryid']);
$category = mysql_fetch_array($categories);
/**
Now we have everything we need. (Assuming that the main page name (VBulletin Community Forum) remains always the same). So here we go:
*/
echo '<a href="index.php">VBulletin Community Forum</a> > <a href="category.php?c='. $category['categoryid'] .'">'. $category['name'] .'</a> > <a href="page.php?pid='. $page['pageid'] .'">'. $page['title'] .'</a>';
This is just an example of course. But I think it would be easier this way.
Chousho
Sun 24th Sep '06, 3:50pm
$sql = @mysql_query("SELECT title FROM page WHERE pageid = $oneup");
$oneup is a resource, and cannot be used like that. Try $row['pid'] instead.
Also note that $title is inside the loop and will be overwritten each time. So at the end it will only hold the last value from the loop. You can add a dot infront of the equal sign, so it will add each title from the loop to the current string.
$title .= $data['title'];
I'm not sure if you're over-complicating this. I think it would be easier if you had a single table for the categories, such as General, and a single table for the pages. Then you can place each page easily into a category assigning it to a number which is the category ID.
Here an example how I'd do it.
$query = mysql_query("SELECT * FROM page WHERE pageid = ". $id);
$page = mysql_fetch_array($query);
/**
Now $page hold all data for this page such as the categoryid, title, content, etc... So let's go ahead and grab the category title
*/
$categories = mysql_query("SELECT * FROM categories WHERE categoryid = ". $page['categoryid']);
$category = mysql_fetch_array($categories);
/**
Now we have everything we need. (Assuming that the main page name (VBulletin Community Forum) remains always the same). So here we go:
*/
echo '<a href="index.php">VBulletin Community Forum</a> > <a href="category.php?c='. $category['categoryid'] .'">'. $category['name'] .'</a> > <a href="page.php?pid='. $page['pageid'] .'">'. $page['title'] .'</a>';
This is just an example of course. But I think it would be easier this way.
The reason I chose this way, instead of how others might normalize it, is because I'm not sure exactly how deep the categories will go.
For example, my site will be about Asian movies, as well as info on Japanese culture, so a category could go like
Main Site > Japan > Cities > Osaka > Dialect
However, even though I have it already set up for all of these to be in one table, I could take some time and redo it.
Thanks for the code though, I didn't realize it was being over-written each time. I suppose if I had looked at it and though, I may have figured it out, but more than likely not. You seem to be quite knowledgeable as well, so thank you very much for taking your time to help me, who does not have such knowledge.
I'll monkey around with the code you gave me and hopefully get it to work. I'll post back with results in a couple of hours. For now, I'm taking a nap, haha. Thanks for the reply!
vBulletin® v3.8.0 Beta 3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.