Welcome Guest, Not a member yet? Register   Sign In
correct way to display a results in a grid in the view - really easy q!
#1

[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
#2

[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
#3

[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.
#4

[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.
#5

[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




Theme © iAndrew 2016 - Forum software by © MyBB