Welcome Guest, Not a member yet? Register   Sign In
SOLVED: Add <div> around every 10 queried results?
#1

[eluser]Joakim_[/eluser]
Hi. I wondering if it's possible to add a <div> around every 10 queried results, without using multiple queries.

Let's say there are 22 results from the query and I want something like this:

Code:
<div class="item_holder">
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
</div>

<div class="item_holder">
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
</div>

<div class="item_holder">
<li>&lt;?php echo $row->item ?&gt;</li>
<li>&lt;?php echo $row->item ?&gt;</li>
</div>

Thank you very much for your help!Smile
#2

[eluser]Dagobert Renouf[/eluser]
I would add a $i variable that you would increment at each returning row.
Then you check :
Code:
if(is_int($i / 10) == TRUE)
to echo your div.

However there's probably a better way to do this.
#3

[eluser]Joakim_[/eluser]
[quote author="Dagobert Renouf" date="1214331108"]I would add a $i variable that you would increment at each returning row.
Then you check :
Code:
if(is_int($i / 10) == TRUE)
to echo your div.

However there's probably a better way to do this.[/quote]

Thank you a lot for your reply and help. I'm not sure where to add the $i variable?

I have the PHP code like this:

Code:
&lt;?php foreach($query->result() as $row): ?&gt;

<li>&lt;?php echo $row->item ?&gt;</li>

&lt;?php endforeach; ?&gt;

Thank you!Smile
#4

[eluser]xwero[/eluser]
You can use the key of the array
Code:
&lt;?php foreach($query->result() as $i=>$row): ?&gt;
&lt;?php if(is_int($i / 10) == TRUE): ?&gt;</div><div class="item_holder">&lt;?php endif ?&gt;
<li>&lt;?php echo $row->item ?&gt;</li>
&lt;?php endforeach; ?&gt;

But i wouldn't recommend to put divs in a list. If you want to put a wrapper around every 10 items you better do
Code:
<ul>
<li><ul> &lt;!-- start first group --&gt;
&lt;?php foreach($query->result() as $i=>$row): ?&gt;
&lt;?php if(is_int($i / 10) == TRUE): ?&gt;</ul></li><li><ul>&lt;?php endif ?&gt; &lt;!-- end group and start a new --&gt;
<li>&lt;?php echo $row->item ?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul></li> &lt;!-- end last group --&gt;
</ul>
This way you can identify the groups by the list that is in a list item. And you have good html code Wink
#5

[eluser]Joakim_[/eluser]
It works! Smile Thank you a lot.




Theme © iAndrew 2016 - Forum software by © MyBB