...

View Full Version : Arrays problem



denied
04-05-2007, 12:08 PM
I have this database table:
5215

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?

iLLin
04-05-2007, 03:56 PM
You should change the title to include smarty, and it looks like some of your php is wrong. Looks like your assigning your menu over and over in your while loop. Give me a min to look at it.

iLLin
04-05-2007, 04:00 PM
Ok a few questions. Does each menu item have its own ID number? Or do they all have 1 for main menu and 2 for submenu? How are you linking the submenu to the appropriate main menu?

denied
04-05-2007, 06:31 PM
Yes, each menu item have its own ID number. The submenu is linking to the parent menu through the PARENTID - if the PARENTID is 2, it is linking with the menu where ID is 2.
The problem is that for a menu it returns all of submenu's, not only the submenu's with the parentid=n.

iLLin
04-05-2007, 06:39 PM
Why not do this



$query = "select * from domnei";
while($row = mysql_fetch_array()) {
if(is_main_menu) {
$menu[$row['parent_id']['main'] = main_id;
} else { //submenu
$menu[$row['parent_id']]['submenu'][] = sub_id;
}
}

foreach($menu as $key => $value) {
//run your menu here
}


I'm sure there is a better way but now you only have "1" query and not many.

denied
04-10-2007, 06:16 PM
I've solved that problem. Here are the code, I'm posting it for other users with the same problem.
The PHP code is:

$menu=array();
$query="SELECT * FROM domenii WHERE parentid=0";
$resursa=$obj->query($query);
while($row=$obj->fetcharray($resursa)){
$query1="SELECT * FROM domenii WHERE parentid=".$row['id'];
$resursa1=$obj->query($query1);
if($obj->numrows($resursa1)>0){
$submenu=array();
while($row1=$obj->fetcharray($resursa1)){
$submenu[]=array('type'=>$row1['type'],
'id'=>$row1['id'],
'nume'=>$row1['nume'],
'title'=>str_replace(' ','-', $row1['nume']));
}
}else{$submenu='0';}

$menu[]=array('type'=>$row['type'],
'id'=>$row['id'],
'nume'=>$row['nume'],
'title'=>str_replace(' ','-', $row['nume']),
'submenu'=>$submenu);
}

$obj->assign('menu', $menu);

The HTML (with smarty) code:

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

Thank you again for your help.

iLLin
04-10-2007, 06:25 PM
Man just use one query and pack your menu into a multi-dimensional array. If you have 6 main menu's, thats 7 queries... Your initial and each additional check for submenu's.

Compact it like this:


['main_link1'] => Array (
['key1'] => value
['key2'] => value
['submenus'] => Array (
['sublink1'] => Array ()
['sublink2'] => Array ()
)
['main_link2'] => Array (
['key1'] => value
['key2'] => value
['submenus'] => Array (
['sublink1'] => Array ()
['sublink2'] => Array ()
)


EDIT: That looks like what you end up doing anyway.

aedrin
04-10-2007, 08:31 PM
Hence this:


I've solved that problem

Should really be:


I've solved that symptom

It's easy to use a shortcut. It's not easy to be a good programmer.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum