Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Nav links for forum sections

    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 .

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Anyone able to help?

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Is there a function in php for loop a bunch of actions until a statment is true?

    EDIT: Duh, while.

    PHP Code:
    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.
    Last edited by tomharto; 05-25-2011 at 01:44 PM.

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    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.
    PHP Code:
    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($forumstrue);
        foreach (
    $forums as $key => $value) {
            echo 
    " > <a href='?f=".$key."'>".$value."</a>";
        }


  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    $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,
    PHP Code:
    $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
    PHP Code:
    $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.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    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

  • #8
    Regular Coder
    Join Date
    May 2007
    Posts
    100
    Thanks
    16
    Thanked 11 Times in 11 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •