Welcome Guest, Not a member yet? Register   Sign In
Foreach loop to show list items while only showing list title once if user has permission to see such list items
#1

[eluser]Unknown[/eluser]
I have a situation where I have a user's specific array of permissions to specific links defined in a variable inside My_Controller.php. I also have a multidimensional array of links specified in the config file (application/config). Now what is required is to loop through the permissions comparing them against the links array and printing out only those links that the user has permissions to while grouping the links according to their titles as defined in the config file. I have managed to loop through but the problem is that it displays the title after each list item instead of printing the title once with a list of list items under it. I checked through a similar problem and found out that the solution approach is the same but the problem comes where I have to loop through the permissions array which means I have three nested loops instead of two. Also is there any performance trade-off in having three nested loops? Is there maybe a better way of going about this situation?

This is the expected output;

Manage
Users
Edit Users
Reception

Tools
Settings
Edit Settings

This is what I'm getting;

Manage
Users
Manage
Edit Users
Manage
Reception

Tools
Settings
Tools
Edit Settings

Here is the config file:

Code:
$config['nav']['manage'] = array(
    'users' => 'auth_title_users',
    'users/edit' => 'auth_title_users_edit',
    'reception' => 'auth_title_reception'
);

$config['nav']['tools'] = array(
    'settings' => 'auth_title_settings',
    'settings/edit' => 'auth_title_settings_edit'
);

Here's the code in my header.php (application/views/includes);

Code:
// $navs contains the multidimentional array of the defined links according to the config file
$navs = $this->config->item('nav', 'tank_auth');

// $menu_items is the array containing permissions the user has from the db
foreach($menu_items as $menu_item){
    $val = $menu_item;

    if($navs){
    foreach($navs as $k => $v){
        if(is_array($v)){
            $header_written = FALSE;
            foreach($v as $key => $value){
                // compare permissions against the links
                if($val == $key){  
                    if (!$header_written){
                        $section_hr = lang('auth_section_'.$k);
                        echo "\t<h3>".$section_hr."</h3>\n";
                    }
                    $list_item = lang('auth_title_'.$key);
                    echo "\t<ul>\n";
                    echo "\t\t<li>".$list_item."</li>\n";
                    $header_written = TRUE;
                }          
            }
        }
        else{
            $header_written = FALSE;
        }      
        if ($header_written){
            echo "\t</ul>\n";
        }    
    }
}
}


Messages In This Thread
Foreach loop to show list items while only showing list title once if user has permission to see such list items - by El Forum - 02-28-2014, 03:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB