Reak I post too much
Reputation: 0
Joined: 15 May 2007 Posts: 3496
|
Posted: Mon Jul 21, 2008 2:39 pm Post subject: [PHP] Array problem |
|
|
Hey,
Well I'm really stuck.
I'm trying to build a dynamic menu with a template engine (Smarty).
It works perfectly with my static array:
For example:
| Code: | | $content->assign('menu', array(array('Main-Menu' => array("http://www.google.de" => "Google.de", "http://www.google.com" => "Google.com")))); |
template:
| Code: | {foreach name=menu item=kontakt from=$menu}
{foreach key=menutitle item=value from=$kontakt}
<center><u>{$menutitle}</u></center>
{foreach key=link item=title from=$value}
<center><a href="{$link}">{$title}</a></center>
{/foreach}
{/foreach}
{/foreach} |
Will display:
Works fine.
But I want to read the menu out of the MySQL.
So there is a table for Menu-Sections (Main-Menu in this case) and a table for MenuItems (google.de, google.com in this case).
So my function so far is:
| Code: | function GetMenu($sql){
$result = $sql->query("SELECT * FROM site_menu ORDER BY position ASC");
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$title = $row['title'];
$menuresult = $sql->query("SELECT * FROM site_menuitems WHERE menuid= $id ORDER BY position ASC");
if(mysql_num_rows($menuresult) > 0) {
while($menurow = mysql_fetch_assoc($menuresult)){
$temp[] = array($menurow['link'] => $menurow['title']);
}
}
$menu[] = array($title => $temp);
}
}
return $menu;
} |
Displays
| Quote: | Main-Menu
Array
Array |
Then I found it it's because of this line
| Code: | | $menu[] = array($title => $temp); |
Because when I'm using this:
| Code: | | $menu[] = array($title => $temp[0]); |
It shows the Main-Menu with the first Google link (.de in this case)
How can I fix this?
Edit:
Ohwut, I finally got it. After like 1.5 hours of trying I finally got it.
| Code: | | $temp[] = array($menurow['link'] => $menurow['title']); |
=
| Code: | | $temp[$menurow['link']] = $menurow['title']; |
(Mod can close)
|
|