formatting query results |
[eluser]gafro[/eluser]
Hey Guys and Gals, Looking for some info with the results received from a query. overview example: DB (table name = items) id name 1 titleone 2 titletwo 3 titlethree 4 titlefour Controller $this->db-get('items'); view <ul> <?php foreach($items-result() as $item):?> <li><?=$item->titleon?></li> <?php endforeach;?> </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
[eluser]Steve Grant[/eluser]
[quote author="gafro" date="1223907308"]<?php foreach($items-result() as $item):?> <li><?=$item->titleon?></li> <?php endforeach;?> </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>
[eluser]John_Betong[/eluser]
Try: Code: <?php foreach($items-result() as $item):?>
[eluser]gafro[/eluser]
@Steve grant - Cheers I'll give that a go. @john Betong - That is a solution for that particular example. I would need to do a check for itemsix, itemnine and so on. Thanks for the help. If there's another way to actually access the array index let me know ![]() Cheers everyone. Gafro
[eluser]John_Betong[/eluser]
Hi Gafro, Try this: Controller: Code: //======================================================== View: Code: <div style='width:500px; margin:1em auto 14em; scroll:auto; border:solid 1px #ddd; text-align:left'> You can see the results Here
[eluser]xwero[/eluser]
[quote author="Steve Grant" date="1223910304"] 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> You can do it with a foreach loop by using the key. Code: <ul><?php foreach($items as $i=>$item): ?>
[eluser]gafro[/eluser]
Excellant. I think that's what I was looking for. Thanks for the help all |
Welcome Guest, Not a member yet? Register Sign In |