Jump to content

Recommended Posts

Hello everyone,
[img src=\"http://worcesterwideweb.com/work/dam.jpg\" border=\"0\" alt=\"IPB Image\" /]

Im trying to make submenus for a cms im playing with, but i cant find the correct SQL SELECT im suppose to use to get the submenu items. basicly I want it to show up like this

Home
About us
Contact
Test Page
- test page sub

and for each sub to be categorized under its parent. As you see i have a parent column with the id of the items parent. What sql statement would I use to select all subitems of the parent?

I have the code working for my parents but not for the child, here is the current child code:

[code]
       $query3 = $database->query("SELECT * FROM pages WHERE parent = id");

while ($list3 = mysql_fetch_array($query3))
{
               $id = stripslashes($list3['id']);
        $date = stripslashes($list3['date']);
        $title = stripslashes($list3['title']);
        $linktitle = stripslashes($list3['linktitle']);
        $pagename = stripslashes($list3['pagename']);
        $description = stripslashes($list3['description']);
        $menuitem = stripslashes($list3['menuitem']);
        $parent = stripslashes($list3['parent']);
            
    
        $items .=  '<div id="anylinkmenu'.$h++.'" class="anylinkcss"><a  href="'.$siteroot.'pages/'.$id.'/'.$pagename.'.html">'.$linktitle.' </a></div>';
        echo $items;
        } [/code]

any help is greatly appreciated thanks :)
Link to comment
https://forums.phpfreaks.com/topic/10277-making-sub-menus-finding-parent/
Share on other sites

maybe something like this would work

SELECT
title,
(SELECT title FROM table WHERE parent = id) AS submenu
FROM
table
WHERE
parent = 0

my logic says select only the items with no parent and select their children. however,t hat subquery might return more than one result - build from here
Here's a bit that I use. There are some class settings and in here which you can remove and/or add your own. If you dont use CSS you can also see where I have added FONT tags.

Maybe it will give you a step in the right direction for yours. I was having problems also until I figured out the right SELECT statment, well actually it's that magical ORDER BY statement.

In my table, I have every entry with a 'project' and a 'subproject'. Similar to a 'catagory' and 'subcatagory' others might use.

Essentially, I am take the now ordered list and displaying the 'project' first, then the 'subproject'. After that each item (url and name) are listed under the 'subproject'. Once all the items are listed, it adds the next 'subproject' and it's items. This continues until there are no additional 'subproject' for that 'project'. Then it proceeds to process the next 'project' and so on.

This works well for me. I used this for creating a dynamic navigation menu.

[code]
//Row 0 - id
//Row 1 - name
//Row 2 - type
//Row 3 - url
//Row 4 - project
//Row 5 - subproject
  
//Retrieve Links and build Navigation Menu for Group Links
$result = mysql_query ("SELECT * FROM yourtablename ORDER BY project, subproject ASC")
  or die ("Query failed");
while($row=mysql_fetch_row($result))
{
If ($currentgroup != $row[4]) {
echo '</ul>';
    echo '<P align="left"><U><STRONG><FONT color="orange" size="2">'. $row[4] .'</U></FONT></STRONG><BR>';
}
    If ($currentsub != $row[5]) {
        If ($currentgroup == $row[4]) {
}    
    echo '<span class="topictitle">'. $row[5] .'</span><br>';
$currentsub = $row[5];
}
echo ('<span class="gensmall"><A href="'.$row[3].'"><li>'.$row[1].'</A></span>');
echo '<br>';
$currentgroup = $row[4];
}
echo '</ul>'; [/code]
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.