TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-21-2008, 11:35 AM   #1 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default Menu with only one query?

Hi, i'm dealing with the following issue.
I'm willing to output a menu of two levels.

If an item contains a parentId it is a subitem, if not; it is a 'main' item.

I want to output this in a nested list. How to do this with only one query?
maZtah is offline  
Reply With Quote
Old 02-27-2008, 10:43 AM   #2 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Okay, let's clearify with a working example (the ugly way with more than one queries):

PHP Code:
            <div id="nav">
<?php
$szQuery 
"SELECT id, name FROM menu WHERE parentId = 0 ORDER BY rank ASC";
$pResult mysql_query($szQuery) or die(mysql_error());

if (
mysql_numrows($pResult))
?>
                <ul>
<?php
    
while($aRow mysql_fetch_array($pResult))
    { 
?>
                    <li<?php echo ($aRow['id'] == $iMenu ' class="active"' ''); ?>>
                        <a href="index.php?menuid=<?php echo $aRow['id']; ?>"><?php echo $aRow['name']; ?></a>
<?php    $szSubQuery "SELECT id, name FROM menu WHERE parentId = {$aRow['id']} ORDER BY rank ASC";
        
$pSubResult mysql_query($szSubQuery) or die(mysql_error());
        
        if (
mysql_numrows($pSubResult))
        { 
?>
                    
                        <ul>
<?php        while($aSubRow mysql_fetch_array($pSubResult))
            { 
?>
                            <li<?php echo ($aSubRow['id'] == $iMenu ' class="active"' ''); ?>><a href="index.php?menuid=<?php echo $aSubRow['id']; ?>"><?php echo $aSubRow['name']; ?></a></li>
<?php        ?>
                        </ul>
                    </li>
<?php    }
        else
        { 
?>
                    </li>
<?php    }
    } 
?>
                    <li<?php echo ($szTitle == 'Contact' ' class="active"' ''); ?>>
                        <a href="contact.php">Contact</a>
                    </li>
                </ul>
<?php
    
?>
            </div>
How would I do this with only one query? :)
maZtah is offline  
Reply With Quote
Old 02-27-2008, 11:32 AM   #3 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

Basicly you select all the items from your menu table, and then let php work it out.
Hope it helpes.
PHP Code:
<?php

/// Select from the table
/// Delimiters if parent = 0 = main. if parent != 0 sub child of 

while ( $r mysql_fetch_array$result) ) {
    if ( 
$r['parent'] == 0) {
            
$parent[$r['id']] = $r;
    } else {
        
$parent[$r['parent']]['subpage'][] = $r;
    }
}
// So you will have thin in your array
/*

 [1] => array( 'id' => '1',
               'name' => 'Main',
               'subpage' => array ( [0] => array( 'id' => '3',
                                                  'name' => 'Main Sub Page 1'
                                                 ),
                                    [1] => array( 'id' => 12,
                                                  'name' => 'Main Sub Page 2'
                                                 )
                                   )
             ),
   [2] .....
*/

foreach ( $parent as $id => $data ) {

    echo 
$data['name'];
    echo 
"br";
    
    
$subpage $data['subpage'];
    if ( 
is_array$subpage) ) {
        foreach ( 
$subpage as $_i => $sub ) {
                echo 
$sub['name'];    
                                echo 
"br";
        }
    }
}

/* OUTPUT ----

Main
--- Main SubPage 1
--- Main SubPage 2
Menu Item 2
Menu Item 3
... Menu Item 3 SubPage 1
... Menu Item 3 SubPage 2

*/
?>
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
The Following 2 Users Say Thank You to abiko For This Useful Post:
DeMo (02-28-2008), maZtah (02-28-2008)
Old 02-28-2008, 12:31 AM   #4 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Thanks abiko, that was what I was looking for. I just couldn't come up with it.

Cheers!
maZtah is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:16 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design