CodeIgniter Forums

Full Version: From view to controller. Moving logic from view to controller.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]behnampmdg3[/eluser]
Hey, I need to clean my disgusting view from php. Please give me tips. Thanks
Code:
<?php
$counter=0;
foreach($gallery_photos as $image)
{
  $counter++;
  ?>
   <a href="&lt;?php echo base_url();?&gt;gallery" class="photo photo&lt;?php echo $counter;?&gt;">
    <div class="photoChopper"  echo base_url().'images/events/'.$image['photo'];?&gt;_square.jpg) center center no-repeat scroll;"></div>
    &lt;?php /*?&gt;<img src="&lt;?php echo base_url().'images/events/'.$image['photo'];?&gt;_square.jpg" alt="Gallery Pictures" />&lt;?php */?&gt;
   </a>
  &lt;?php
}
?&gt;

El Forum

[eluser]Aken[/eluser]
First, if you have a block of commented code, just remove it. All it does is bloat the file.

Your code isn't that awful, just has a lot of opening / closing PHP. I would iterate through your array in the controller first, and move the counter and base_url() logic there. Also, if your gallery link won't change, you can define it once instead of calling base_url() over and over (more a performance issue than how nice it looks).

Also, base_url() accepts a string that's appended to the URL. It looks cleaner, IMO:

Code:
echo base_url() . 'images/events/' . $image['photo']; ?&gt;_square.jpg

// Compared to:

echo base_url('images/events/' . $image['photo'] . '_square.jpg');