Jump to content

Array problem


denied

Recommended Posts

I have this table in the database:

php.JPG

 

I am storing here the menu's from my website. If PARENTID=0 there are no submenus from that menu. If PARENTID=2, that menus are children of menu wich have the ID=2.

I've made a script wich select the records wich have ID=0 and echo them, then, before the while close, I've done another select wich select the rows with PARENTID=ID (for example, when the first select extract the row from ID=2, the second select detect that there are 2 rows that have PARENTID=2 and echo that rows).

The result was something like that:

Menu Submenu

1

            5

2

            3

            4

5 is submenu of menu 1; 3 and 4 is submenu of menu 2

 

I want to do this with arrays, because I want to use smarty for displayng the data. I've made the same selects and put the data in 2 arrays but when I want to display the arrays, from menu 1 I have all submenus (3,4,5), for menu 2 I have the same, all submenus (3,4,5). I will show you the code above:

 

In PHP I have:

$query="SELECT type, id, nume FROM domenii WHERE parentid=0"; 
$resursa=$obj->query($query); 
$menu=array(); 
$i=0; 

while($row=$obj->fetcharray($resursa)){ 
    $tmp=array('type'=>$row['type'],  
                'id'=>$row['id'], 
                'nume'=>$row['nume'], 
                'title'=>str_replace(' ','-', $row['nume'])); 
    $menu[$i++]=$tmp; 
     
    /*begining of submenu's*/         
    $query1="SELECT type, id, nume, parentid FROM domenii WHERE parentid=".$row['id']; 
    $resursa1=$obj->query($query1); 
    if($obj->numrows($resursa1)>0) 
    { 
        $submenu=array(); 
        $j=0; 
        while($row1=$obj->fetcharray($resursa1)){ 
        $tmp1=array('type'=>$row1['type'], 
                        'id'=>$row1['id'], 
                        'nume'=>$row1['nume'], 
                        'title'=>str_replace(' ','-', $row['nume'])); 
        $submenu[$j++]=$tmp1; 
        } 
        $obj->assign('submenu', $submenu); 
    } 
    /*end of submenu's*/ 
    $obj->assign('menu', $menu); 
} 

 

and in the template (with smarty):

<ul id="nav">
{section name=i loop=$menu}
	<li><a href="index.php?type={$menu[i].type}&id={$menu[i].id}&title={$menu[i].title}">{$menu[i].nume}</a>
	{if is_array($submenu)}
		<ul>
		{section name=j loop=$submenu}
		<li><a href="index.php?type={$submenu[j].type}&id={$submenu[j].id}&title={$submenu[j].title}">{$submenu[j].nume}</a></li>
		{/section}
		</ul>
	{/if}
	</li>
	{/section}
</ul>

 

What can I do to make it work?

Link to comment
https://forums.phpfreaks.com/topic/45709-array-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.