Welcome Guest, Not a member yet? Register   Sign In
The View and multidimensional array
#1

[eluser]quince[/eluser]
Hi people,
i have problem with arrays
i'm using multidimensional array in controller file

Code:
function mymenu($opt){
      switch ($opt){
        case 'company':
           $data['submenu'] = array("link" =>
              array(0=>"Somethin",
                    1=>"Somethin_new",
                    2=>"Somethin_again"),
           "href" =>
              array(0=>"/webcompany/index.php/topmenu/",
                    1=>"/webcompany/index.php/topmenu/aaa",
                    2=>"/webcompany/index.php/topmenu/bbb")
           );
   break;
      }
       case 'places':
       .
       .
       .
   }

  $this->load-view('topmenu',$data);

now, what is the code in the topmenu view file
cause, this don't work
Code:
foreach($submeni as $item):
   echo $item['href'][$i];
   echo $item['link'][$i];
   $i++;
    endforeach;
href and link are unknown

help me please
thanks
#2

[eluser]henrihnr[/eluser]
you should do like this
Code:
foreach($submenu as $item):
   echo $item[0];
   echo $item[1];
   echo $item[2];
    endforeach;

'link' and 'href' already accessed from foreach loop...
#3

[eluser]wr5aw[/eluser]
It might be easier to setup your menu items in a config file. That way you can modify menu items without mucking around with the controller code.

Consider the following config file in config/menu.php:
Code:
<?php
// Company Menu
$config['menus']['company']['title'] = 'Company Title';
//
$config['menus']['company']['items']['somethin']['text'] = 'Somethin';
$config['menus']['company']['items']['somethin']['href'] = '/webcompany/index.php/topmenu/';
//
$config['menus']['company']['items']['somethin_new']['text'] = 'Somethin new';
$config['menus']['company']['items']['somethin_new']['href'] = '/webcompany/index.php/topmenu/aaa';
//
$config['menus']['company']['items']['somethin_else']['text'] = 'Somethin else';
$config['menus']['company']['items']['somethin_else']['href'] = '/webcompany/index.php/topmenu/bbb';
//
//
//Places Menu
$config['menus']['places']['title'] = 'Places Title';
//
$config['menus']['places']['items']['someplace']['text'] = 'Places';
$config['menus']['places']['items']['someplace']['href'] = '/webcompany/index.php/places/';
//
$config['menus']['places']['items']['someplace']['text'] = 'Someplace';
$config['menus']['places']['items']['someplace']['href'] = '/webcompany/index.php/places/aaa';
//
$config['menus']['places']['items']['someplace']['text'] = 'Someplace';
$config['menus']['places']['items']['someplace']['href'] = '/webcompany/index.php/places/aaa';
Then the menu template in views/menu.php:
Code:
<div class = "menu">
&lt;?php
foreach ($menus as $menu_id=>$menu)
{
    echo "\t\t<h3 id='$menu_id'>".$menu['title']."</h3>\n";
    echo "\t\t<ul id='$menu_id'>\n";
    foreach ($menu['items'] as $item_id=>$item)
    {
        echo "\t\t\t<li id='$item_id'>" . "<a href='" . $item['>" . $item['text'] . "</a></li>\n";
    }
    echo "\t\t</ul>\n";
}
?&gt;
    </div>
And a main template file in views/main.php:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Testing Menus&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<div class="menus">
&lt;?php echo $menus; ?&gt;
</div>
<div class="content">
&lt;?php echo $content; ?&gt;
</div>
&lt;/body&gt;
&lt;/html&gt;
And finally, the controller welcome.php:
Code:
&lt;?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->config('menus');
        $data['menus'] = $this->config->item('menus');
        $data['menus'] = $this->load->view('menu', $data, TRUE);

        $data['content'] = "Your main content here";
        $this->load->view('main', $data);
    }
}
Here's the generated html:
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Testing Menus&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<div class="menus">
    <div class = "menu">
        <h3 id='company'>Company Title</h3>
        <ul id='company'>
            <li id='somethin'><a href='/webcompany/index.php/topmenu/'>Somethin</a></li>
            <li id='somethin_new'><a href='/webcompany/index.php/topmenu/aaa'>Somethin new</a></li>
            <li id='somethin_else'><a href='/webcompany/index.php/topmenu/bbb'>Somethin else</a></li>
        </ul>
        <h3 id='places'>Places Title</h3>
        <ul id='places'>
            <li id='someplace'><a href='/webcompany/index.php/places/aaa'>Someplace</a></li>
        </ul>
    </div>
</div>
<div class="content">
Your main content here</div>
&lt;/body&gt;
&lt;/html&gt;
As you can see, there are a couple of advantages to this method. First, you only have to edit config/menus.php to edit menu entries. Second, it wouldn't be too difficult to port the menu to database entries instead of a config file. And finally, since we're using array keys, which are unique, we can take advantage of that by using them as id params in our html which makes it very easy to style our output using css.
#4

[eluser]quince[/eluser]
what can i say...
thanks man, thanks a lot
i like it Smile
#5

[eluser]wr5aw[/eluser]
Arg... The editor is fubaring the code. For some reason I can't get it to display the href in the menu template properly.

The url for the item link is supposed to be:

$item['href']




Theme © iAndrew 2016 - Forum software by © MyBB