Welcome Guest, Not a member yet? Register   Sign In
Code Igniter, how to do it the right way?
#1

[eluser]Sector[/eluser]
Hello there, I'm using Code Igniter and I'm absolutely loving it. Yet when I make my site, I tend to do stuff the old-fashioned way (i.e.: very badly). But when I stop and think about what I'm doing, I do it anyway because I can't think up a better way to do it.

Note I said better, because I can surely think up different ways Big Grin

For example, this is in one of my view files:

Code:
<ul>
                &lt;?php
                
                $links = array(
                    'main', 'files', 'scripts', 'forum', 'admin'
                );
                
                foreach ($links as $name) {
                    echo "<li>";
                    
                    if ($name == $currentpage) {
                        echo anchor($name, $name, array('class' => 'active'));
                    }
                    
                    else {
                        echo anchor($name, $name);
                    }
                    
                    echo "</li>\n";
                }
                
                ?&gt;
            </ul>

And this is in another view file:

Code:
&lt;?php
                
                $currentlanguage = '';
                
                foreach ($scripts->result() as $row) {
                    if ($row->language != $currentlanguage) {
                        $currentlanguage = $row->language;
                        
                        echo '</ul>';
                        echo '<h4>' . $currentlanguage . '</h4>';
                        echo '<ul>';
                    }
                    
                    echo '<li><a href="#'  . $row->name . '">'. $row->name .'</a></li>';
                }
                
                echo '</ul>';
                
                ?&gt;

I've got this terrible feeling that I'm not supposed to do that kind of checking in a view file, in fact there should be very little code. Yet I don't know how to properly handle this according to MVC.

Any help would be greatly appreciated!
#2

[eluser]optimal[/eluser]
I don't think it's _that_ bad.

In your second example, I would describe the code this way: if the group's category changes, display a new header. From my perspective, this is display logic, and not data processing logic. And responsibility for display is rightfully held by a view, which is provided with data by the controller.

I wouldn't want to see that markup in your controller!

MVC is a modular architecture, and a good question to ask is: what happens if my controller needs to support a new type of display? In that case you'd need to create a new view which may not use the same markup, or may not even use HTML. So if your code relates specifically to display, it logically belongs in a view. And if the impact on your controller is minimal, then you likely have a good design.

You might explore using PHP's alternative syntax (http://ellislab.com/codeigniter/user-gui...e_php.html) in your views, but in this case I'm not sure it would improve the readability that much.

Maybe others will have a different perspective or ideas about how to improve your views.
#3

[eluser]Sector[/eluser]
Thanks for answering, I'm glad it's possibly not as bad as I thought it was Smile
#4

[eluser]Dr.Dan[/eluser]
Hi Sector,

You are having a good point that is keeping the code in view as minimal as possible. In fact I think it is a good idea to keep code in view as low as possible. Referring to your first example you could have write it like this:

You could have write this in your controller
Code:
$data['links'] = array(
                    'main', 'files', 'scripts', 'forum', 'admin'
                );

and then you could access it in view by $links .

I also agree with optimal, that whenever you need to use the code in view, use it. And you should not be bother too much with that. Smile




Theme © iAndrew 2016 - Forum software by © MyBB