Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Arrays problem

  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrays problem

    I have this database table:
    Arrays problem-graphic1.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:
    PHP Code:
    $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):
    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($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?
    Last edited by denied; 04-05-2007 at 11:10 AM.

  • #2
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Why not do this

    PHP Code:
    $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.

  • #6
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've solved that problem. Here are the code, I'm posting it for other users with the same problem.
    The PHP code is:
    PHP Code:
    $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:
    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.

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    ['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.
    Last edited by iLLin; 04-10-2007 at 05:27 PM.

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •