PHP beginner |
|
Hallo,
ik heb 2 tabellen in men database:
Page & menu
Page: (ik geef voor de gemakkelijkheid alleen ff de id's mee)
ID
---
1
2
3
4
Menu:
ID | Parent | Child
------------------
1 | 1 | 0
2 | 2 | 3
3 | 2 | 4
Ik wil dus een menu maken via een template parser. Ik heb 4 pagina's: 1 - 2 - 3 - 4
1 en 2 moeten in de menu komen, en als je over 1 gaat, moeten 3 en 4 zichtbaar worden.
De template code:
[start-block menu]
<a href="/index.php"><li class="item">Startpagina
<ul>
[start-block submenu]<a href="{Submenu_item:url}"><li>{Submenu_item:text}</li></a>[end-block submenu]
</ul>
</li></a>
[end-block menu]
[start-block menu] <a href="/index.php"><li class="item">Startpagina [start-block submenu] <a href="{Submenu_item:url}"><li>{Submenu_item:text} </li></a>[end-block submenu] [end-block menu]
De code om de menu op te bouwen:
$query = " SELECT
DISTINCT
parent
FROM
menu
";
$result_fetch_menu_data = $dbConnection->query( $query );
while( list( $parent ) = $dbConnection->fetchArray( $result_fetch_menu_data ) )
{
$query = " SELECT
access_url,
title
FROM
page
WHERE
id = '".$parent."'
";
$result_fetch_menu_parentvalue = $dbConnection->query( $query );
list( $access_url , $title ) = $dbConnection->fetchArray( $result_fetch_menu_parentvalue );
$template->newBlock( 'menu' , array( 'Menu_item:url' => 'index.php?cm_page='.$access_url, 'Menu_item:text' => $title ) );
$query = " SELECT
child
FROM
menu
WHERE
parent = '".$parent."'
";
$result_fetch_menu_child = $dbConnection->query( $query );
while( list( $child ) = $dbConnection->fetchArray( $result_fetch_menu_child ) )
{
if( $child != 0 )
{
$query = " SELECT
access_url,
title
FROM
page
WHERE
id = '".$child."'
";
$result_fetch_menu_childvalue = $dbConnection->query( $query );
list( $access_url_child , $title_child ) = $dbConnection->fetchArray( $result_fetch_menu_childvalue );
$template->newBlock( 'submenu' , array( 'Submenu_item:url' => $access_url_child, 'Submenu_item:text' => $title_child ) );
}
}
}
$query = " SELECT DISTINCT parent FROM menu "; $result_fetch_menu_data = $dbConnection->query( $query ); while( list( $parent ) = $dbConnection->fetchArray( $result_fetch_menu_data ) ) { $query = " SELECT access_url, title FROM page WHERE id = '".$parent."' "; $result_fetch_menu_parentvalue = $dbConnection->query( $query ); list( $access_url , $title ) = $dbConnection->fetchArray( $result_fetch_menu_parentvalue ); $template->newBlock( 'menu' , array( 'Menu_item:url' => 'index.php?cm_page='.$access_url, 'Menu_item:text' => $title ) ); $query = " SELECT child FROM menu WHERE parent = '".$parent."' "; $result_fetch_menu_child = $dbConnection->query( $query ); while( list( $child ) = $dbConnection->fetchArray( $result_fetch_menu_child ) ) { if( $child != 0 ) { $query = " SELECT access_url, title FROM page WHERE id = '".$child."' "; $result_fetch_menu_childvalue = $dbConnection->query( $query ); list( $access_url_child , $title_child ) = $dbConnection->fetchArray( $result_fetch_menu_childvalue ); $template->newBlock( 'submenu' , array( 'Submenu_item:url' => $access_url_child, 'Submenu_item:text' => $title_child ) ); } } }
Wat gebeurt er?
Pagina 1 en 2 worden netjes weergegeven, maar nu heeft zowel pagina 1 als pagina 2 een submenu met 3 en 4 in, terwijl dit normaal alleen pagina 1 mag hebben.
Wie kan me hierbij helpen dit op te lossen?
EDIT:
ik denk zelf dat de fout in de code zit van de HTML zelf. Dat submenu block zit genest in het menu block en dus 2 keer want de menu block wordt 2 keer opgehaald. Hoe kan ik ervoor zorgen dat de submenu block maar één keer wordt opgehaald?
|