CodeIgniter Forums
correct way to display a results in a grid in the view - really easy q! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: correct way to display a results in a grid in the view - really easy q! (/showthread.php?tid=23522)



correct way to display a results in a grid in the view - really easy q! - El Forum - 10-13-2009

[eluser]thiswayup[/eluser]
Hi All

I'm new to MVC and mindful of doing it right as possible for this project I'm working on. Essentially I have a my result and listing it is just fine where I just loop through it but what if i wanted to display it as a grid? Say I wanted to have grid of 3 across and 2 high. I would have in the past done a check if the result was item 3 then I would create a new row.

Am i just over thinking this and this is actually the right approach to take for the view?

Cheers


correct way to display a results in a grid in the view - really easy q! - El Forum - 10-13-2009

[eluser]CroNiX[/eluser]
A little unclear by what you mean by 3 across and 2 high. Can you provide more details? Im thinking you just want to display a table in your view with the data.

In the view, put something like (assuming the array of data you are passing to your view is called '$results':
Code:
<!-- create table -->
<table>
&lt;!-- create column headers --&gt;
<thead>
<tr>
    <th>Name</th>
    <th>Phone</th>
</tr>
</thead>
&lt;!-- display the data --&gt;
<tbody>
&lt;?php foreach($results as $result): ?&gt;
<tr>
    <td>&lt;?php echo $result['name']; ?&gt;</td>
    <td>&lt;?php echo $result['phone']; ?&gt;</td>
</tr>
&lt;?php endforeach; ?&gt;
</tbody>
</table>

This would create something similar to:
Name Phone

John 555-555-1212
Sam 555-555-1213
Chris 555-555-1214


correct way to display a results in a grid in the view - really easy q! - El Forum - 10-13-2009

[eluser]thiswayup[/eluser]
sorry, I meant say if I had 6 products, I wanted to display on the top row, 3 products then on the second row the next 3 products.


correct way to display a results in a grid in the view - really easy q! - El Forum - 10-13-2009

[eluser]jedd[/eluser]
MVC doesn't change the algorithm here - you'd do this in your view, and you'd do it by cycling through a counter to trigger a new row, as you suggest, every third item.

I gather you're okay to write that code, yeah?

If you want to get funky, you could make it a code partial view that you call in from your controller in different places, which means generalising the view. In my (humble) experience this can be more trouble than it's worth, though .. depends on the size of your codebase I guess.


correct way to display a results in a grid in the view - really easy q! - El Forum - 10-17-2009

[eluser]thiswayup[/eluser]
yip, thanks for clarity. I'm all good for the code.

re: the second point, i was actually thinking of that as i have seen some discussion on that exact same approach. But for now, I'll follow the KISS concept