Welcome Guest, Not a member yet? Register   Sign In
Efficient Menu
#1

[eluser]georgerobbo[/eluser]
Is there a more efficient way to do a basic menu than below.

Code:
<div id="menu">
            <ul>
                <li><a href="&lt;?=base_url()?&gt;">Home</a></li>
                <li><a href="&lt;?=site_url()?&gt;/focus">Focus</a></li>
                <li><a href="&lt;?=site_url()?&gt;/projects">Projects</a></li>
                <li><a href="&lt;?=site_url()?&gt;/resources">Resources</a></li>
                <li><a href="&lt;?=site_url()?&gt;/updates">Updates</a></li>
                <li><a href="&lt;?=site_url()?&gt;/about">About</a></li>
            </ul>
        </div>

I was thinking of using an array and creating a loop, but in PHP you can't use foreach twice so how would I create the loop from two arrays containing the links and the names? *I'm a beginner to PHP and MVC.
#2

[eluser]Spicer[/eluser]
This is how I often deal with it.

Code:
<php $list = array("home" => "Home", "focus" => "Focus"); ?&gt;
<div id="menu">
            <ul>
                &lt;?php foreach($list AS $url => $name) : ?&gt;
                <li><a href="&lt;?=base_url()?&gt;/&lt;?=$url?&gt;">&lt;?=$name?&gt;</a></li>
                &lt;?php endforeach; ?&gt;
            </ul>
</div>
#3

[eluser]Clooner[/eluser]
Quote:but in PHP you can't use foreach twice so

Yes, you can use a foreach inside a foreach.

Here is a function to generate a ul list
Code:
function ulgen($data)
{
    echo "<ul>\r\n";
    foreach ($data as $key=>$value)
    {
        echo "<li>";
        if (is_array($value))
            $this->ulgen($value);
        else
            echo '<a href="'.$key.'">'.$value.'</a>';
        echo "</li>";
    }
    echo "</ul>\r\n";
}
Use an array with keys as links and values as titles of that link
#4

[eluser]designfellow[/eluser]
Hi,
you can use
$links=array('key1'=>'value1','key2'=>'value2');
echo "<div><ul>";
foreach($links as $text=>$url)
{
echo "<li>".anchor($url,$text)."</li>";
}
echo "</div>";

Happy Coding,
DesignFellow
#5

[eluser]Clooner[/eluser]
Woot three times the same solution within minutes. Too bad Spicer beat us to it, Designfellow Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB