Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Thanked 0 Times in 0 Posts

    creating a menu using php/mysql

    I created a table via mysql called "sections" and inside it there are 4 fields

    • id
    • name
    • catid
    • incfile

    Now on index.php I have ouputted the main menu links with catid=0 and the menu links with catid=3,4 and 6.

    This is the menu

    Home - id=1, catid=0
    About us - id=2, catid=0
    Services - id=3, catid=0
    Products - id=4, catid=0
    Maintenance - id=5, catid=0
    Projects - id=6, catid=0
    Contact us - id=7, catid=0
    Search - id=8, catid=9999 (Not shown on the menu)
    Client Login - id=9, catid=9999 (Not shown on the menu)
    Software Development - id=10, catid=3 (referring to the id of Services, submenu of services)
    Networking - id=11, catid=3 (referring to the id of Services, submenu of services)
    Window - id=19, catid=10 (referring to the id of Software Development, submenu of Software Development)

    Now this is the index.php code, I was able to output the main menu links and the submenu links but I can't figure out how to bring the sub-submenu links. Can you please help ?

    PHP Code:
    # connection to the db
    include ("includes/conn.php");?>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Development Networking</title>

    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

    <table width="100%" height="100%" border="0" cellpadding="5" cellspacing="5">
        <td width="300" valign="top" bgcolor="#999999">
    <?php $conn mysql_query("SELECT * FROM sections WHERE catid = '0' order by id asc "); 
    $rows mysql_num_rows($conn);

    $ccnt '0';
    while (
    $rows $ccnt) {

    $connlist[$ccnt] = mysql_fetch_array($conn); ?>

    <a href="index.php?id=<?php  echo $connlist[$ccnt]['id'?>">
    <?php echo $connlist[$ccnt]['name'] ;?></a>

    $conn2 mysql_query("SELECT * FROM sections WHERE catid = '$varconn' order by name asc "); 
    $rows2 mysql_num_rows($conn2);
     if (
    $rows2 == '0') { echo "<br>"; } else {?>
    while (
    $rows2 $ccnt2) {

    $connlist2[$ccnt2] = mysql_fetch_array($conn2); ?>
    <a href="index.php?id=<?php echo $connlist2[$ccnt2]['id'?>">
    <?php echo $connlist2[$ccnt2]['name'] . "<br>";?>
    $conn3 mysql_query("SELECT * FROM sections WHERE catid = '$varconn2' order by name asc "); 
    $rows3 mysql_num_rows($conn3);
    $ccnt3 '0';
    while (
    $rows3 $ccnt3) {
    $connlist3[$ccnt3] = mysql_fetch_array($conn3);
    <a href="index.php?id"<?php echo $connlist3[ccnt3]['id'?>">
    // there is something wrong with the query
    echo $connlist3[$ccnt3]['name'] . "<br>";

        <td valign="top" bgcolor="#000000">&nbsp;</td>

    // to close the connection with the database after getting what you asked
    mysql_close() ?>

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Thanked 58 Times in 54 Posts
    You want to store a tree structure. Just search for a tutorial on that, which will more than likely help you on the way.


    Posting Permissions

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