Welcome Guest, Not a member yet? Register   Sign In
PHP Scripting in Views
#1

[eluser]Tim Reynolds[/eluser]
Hi,

I have programmed in procedural php before but now decided to get into a framework and CodeIgniter looked like a great choice. Am getting to grips with it well just have one question.
While following a great screen cast by Elliot Haughin he used the following Syntax to output an array in a view,
Code:
<?php if (isset($navigation) && !empty($navigation));?>
    <ul id="nav">
    &lt;?php foreach($navigation as $nav);?&gt;
        <li>
        </li>
    &lt;?php endforeach;?&gt;
    </ul>    
&lt;?php endif;?&gt;

Now i am use to JSTL in Java and this seems the same principle but I could not get it to work. Instead i used echo like so,
Code:
&lt;?php
if (isset($navigation) && !empty($navigation)){
    echo "<ul id='nav'>";
    foreach($navigation as $nav)
    {
        echo "<li><a href='".$nav[' title='".$nav['>".$nav['menu_title']."</a></li>";
     }
     echo "</ul>";
}
?&gt;

Using echo worked fine but why did the other method not work correctly? Is it to do with server config or am i just doing it wrong?
I am running MAMP with default settings and the latest release of CI.

Am sure there is a simple answer.

Thanks for your help in advanced.

Tim
#2

[eluser]Armchair Samurai[/eluser]
AFAIK, the syntax posted is wrong - rather than use a semi-colon to open your conditional, you should use a colon.
Code:
&lt;?php if (isset($navigation) && !empty($navigation)):?&gt; // colon, not semi-colon
    <ul id="nav">
    &lt;?php foreach($navigation as $nav):?&gt; // colon, not semi-colon
        <li>
        </li>
    &lt;?php endforeach;?&gt;
    </ul>    
&lt;?php endif;?&gt;
Alternately, if you prefer bracket syntax:
Code:
&lt;?php if (isset($navigation) && !empty($navigation)){?&gt;
    <ul id="nav">
    &lt;?php foreach($navigation as $nav){?&gt;
        <li>
        </li>
    &lt;?php }?&gt;
    </ul>    
&lt;?php }?&gt;
#3

[eluser]pistolPete[/eluser]
This is also covered by the user guide: Alternate PHP Syntax for View Files.
#4

[eluser]Tim Reynolds[/eluser]
Thank you. I like the look of the alternate PHP Syntax seems much easier.




Theme © iAndrew 2016 - Forum software by © MyBB