Welcome Guest, Not a member yet? Register   Sign In
How to output database to table formated into rows and columns
#6

[eluser]WanWizard[/eluser]
The main reason for putting your I/O code in a model is that is it easy to reuse, and it hides complexity from your controllers. It also allows you to make changes to your database environment, without affecting your controller code (as long as you don't change the return values of the methods in your model).

In your case, create a model, and in that model a method called get_my_pics(). You could pass some variables to the method if you want to make selections, run the query, and have the method return the $query->result().

In the above example, you could then use:
Code:
<? foreach ($this->mymodel->get_my_pics() as $i=>$row): ?>

Also, I don't think comparing $i to "3" (which is a string) is a good idea. And what happens if $i gets to 7? Another issue is that if the query returns an exact multiple of four, you end up with an empty table row with no cells, which doesn't validate.

I would do something like:
Code:
<table>
    &lt;? foreach ($query->result() as $i=>$row): ?&gt;
    &lt;? if ($i % 4 == 0): echo "<tr>"; endif; ?&gt;
    <td>&lt;?=$row->file?&gt;<br/>Photo</td>
    &lt;? if ($i % 4 == 3): echo "</tr>"; endif; ?&gt;
    &lt;? endforeach; ?&gt;
    &lt;? if ($i % 4 != 3): echo str_repeat("<td></td>", 3-($i % 4)); endif; ?&gt;
</table

This is from the top of my head, there are probably better solutions. Also, I never mix html and php (I don't use views), so I'm not sure of the correct syntax.


Messages In This Thread
How to output database to table formated into rows and columns - by El Forum - 08-29-2009, 08:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB