• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
formatting query results

#2
[eluser]Steve Grant[/eluser]
[quote author="gafro" date="1223907308"]<?php foreach($items-result() as $item):?>

<li>&lt;?=$item->titleon?&gt;</li>
&lt;?php endforeach;?&gt;
</ul>


What I'd like to do is add a class to the li on every 3rd row.

<ul>
<li>itemone</li>
<li>itemtwo</li>
<li class="classname">itemthree</li>
<li>itemfour</li>
</ul>

I'm not sure how to get access to the keys from the array the active record has returned.

Can anyone shed some light or point me in the right direction.

Cheers all
Gafroninja[/quote]
Your best bet is probably to use a for loop rather than foreach, as you then effectively create your own array indexing.

Something like...
Code:
<ul>
&lt;?php for($i = 0; $i < count($items); $i++): ?&gt;
  &lt;?php $item = $items->result(); ?&gt;
  <li&lt;?php if(($i + 1) % 3 == 0): ?&gt; class="classname"&lt;?php endif; ?&gt;>&lt;?php echo $item->name; ?&gt;
&lt;?php endfor; ?&gt;
</ul>


Messages In This Thread
formatting query results - by El Forum - 10-13-2008, 03:15 AM
formatting query results - by El Forum - 10-13-2008, 04:05 AM
formatting query results - by El Forum - 10-13-2008, 04:09 AM
formatting query results - by El Forum - 10-13-2008, 04:16 AM
formatting query results - by El Forum - 10-13-2008, 06:44 AM
formatting query results - by El Forum - 10-13-2008, 06:55 AM
formatting query results - by El Forum - 10-13-2008, 07:24 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.