PDA

View Full Version : Nav links for forum sections



tomharto
05-25-2011, 01:16 AM
I need help making the links for a forum (like the ones here, CodingForums.com > Server side > Php). I have the sections names stored in a database with the fields `name`, `parent`, `ID`. e.g.
`name`-`parent`-`ID`
`main`--`0`-------`1 `
`sub1`--`1`-------`2 `

I dont know if theres a better way of doing it so any help is appreciated :).

tomharto
05-25-2011, 02:07 PM
Anyone able to help?

tangoforce
05-25-2011, 02:37 PM
I had to do a similar thing for a barcode inventory i worked on a while back where items needed to be nested (EG something could be inside box A which is inside box B) and the only logical way i could find to do it was to use a parent as you have.

tomharto
05-25-2011, 02:39 PM
Is there a function in php for loop a bunch of actions until a statment is true?

EDIT: Duh, while.



if (isset($_GET['f'])) {
$f = mysql_real_escape_string($_GET['f']);
$Sql = "SELECT * FROM `sections` WHERE `ID` ='$f'";
$Sql = mysql_query($Sql);
while ($row = mysql_fetch_array($Sql)) {
$parent = $row['parent forum ID'];
if ($parent != "0") {
$Sql = "SELECT * FROM `sections` WHERE `ID` ='$parent'";
$Sql = mysql_query($Sql);
while ($row = mysql_fetch_array($Sql)) {
echo " > <a href='?f=".$row['ID']."'>".$row['name']."</a>";
}
}
}
}
So far i have that to display the parent forum catagory, but i cant seem to get it to loop until parent forum ID == 0. Can you help me figure it out? I tried replacing if ($parent != "0") to while but that didnt work.

tomharto
05-25-2011, 04:03 PM
I got it to loop till $parent == 0 however it printed them out in the wrong order, so i did some messing about using an array and eventually this worked.


if (isset($_GET['f'])) {
$f = mysql_real_escape_string($_GET['f']);
$Sql = "SELECT * FROM `sections` WHERE `ID` ='$f'";
$Sql = mysql_query($Sql);
while ($row = mysql_fetch_array($Sql)) {
$parent = $row['parent forum ID'];
while ($parent != "0") {
$Sql = "SELECT * FROM `sections` WHERE `ID` ='$parent'";
$Sql = mysql_query($Sql);
while ($row = mysql_fetch_array($Sql)) {
$forums[$row['ID']] = $row['name'];
$parent = $row['parent forum ID'];
}
}
}
$forums = array_reverse($forums, true);
foreach ($forums as $key => $value) {
echo " > <a href='?f=".$key."'>".$value."</a>";
}
}

abduraooft
05-25-2011, 05:47 PM
$Sql = "SELECT * FROM `sections` WHERE `ID` ='$f'";
$Sql = mysql_query($Sql);
while ($row = mysql_fetch_array($Sql)) {
$parent = $row['parent forum ID'];
while ($parent != "0") {
$Sql = "SELECT * FROM `sections` WHERE `ID` ='$parent'";
$Sql = mysql_query($Sql);
while ($row = mysql_fetch_array($Sql)) {
$forums[$row['ID']] = $row['name'];
$parent = $row['parent forum ID'];
}
}
}
What if when you have one more level? If you are okay with using multiple queries, you could use a recursive function with a global array inside it.
Say,

$arrBC=array();
function getBreadCrumb($id=0){
global $arrBC;
$Sql = "SELECT * FROM `sections` WHERE `ID` =$id LIMIT 1";
$result=mysql_query($Sql);
if(mysql_num_rows($result)){
......
getBreadCrumb($row['parent_id']);
}
else
return;

}

In one of my sites, I've generated an xml file to represent all the rows in the 'forum' table. Then, I can easily get the hierarchical elements by a traversal through the file using DOM functions, like

$dom->load($xmlPath);
$node=$dom->getElementById('id_'.$id);
if(!$node) return;

while($node->nodeName!='root'){
$path[]=array('name'=>$node->getAttribute('name'));
$node=$node->parentNode;
}
$path=array_reverse($path);
return $path;


I "believe" (may be wrong), this would be more effective than running multiple queries. And a drawback of this method is, I need to write procedures for updating the xml elements along with any hierarchy/name change in the 'forum' table.

tomharto
05-25-2011, 09:46 PM
Ive never used XML ad PHP before and seeing as im just making this forum as a learning experience, ill have a go at that see what happens, thanks :)

>ssp-cdr<
05-26-2011, 03:28 AM
There is a way to form a breadcrumb list from tree data stored in a database using only two queries.

Familiarize yourself with the Nested Set Model - http://en.wikipedia.org/wiki/Nested_set_model

Notice how the technique involves a left and a right number for each node in the tree, as well as the parentID of the node. These two extra numbers need to be setup in advance and redone each time a new node is added to the tree. This can be setup to be done automatically.

I've tried searching to find a good tutorial but can't seem to find one which explains the concept and keeps it simple.

If you are really interested I can explain this to you further, just say so. It is the most efficient way of doing breadcrumbs links like you want for your forum.