1

I am trying to get results from database . For now i tried to take out title from a custom table i made . All is working well except that there are only two rows with one title in each in that table but when the result outputs through echo each of them are repeated four times . My code is

<div class="block-content-col">
<ul>
<?php
$collection = Mage::getModel('example/example')->getCollection();
$keys = array_keys($collection->getFirstItem()->getData());
foreach ($keys as $key){ // loop through all the keys (fname, lname, email... 
    foreach ($collection as $obj){//loop throught each object 


?>

<li class="col-item">
<div>
<span>  <?php  echo $obj->getData("title");//get the value for a speficic key. ?></span>
</div>
</li>


<?php
    } 
}
?>
</ul>
</div>

2 Answers 2

1

Looping through the keys has no real use here as far as I can see.

<div class="block-content-col">
<ul>
<?php
$collection = Mage::getModel('example/example')->getCollection();
foreach ($collection as $obj){//loop throught each object 
?>
   <li class="col-item">
   <div><span><?php  echo $obj->getData("title");//get the value for a speficic key. ?></span></div>
   </li>
<?php
}
?>
</ul>
</div>

Would still output a list with the title of each item in the database as, I think, you intended.

0

I think you should reverce your foreach loops

foreach ($collection as $obj){//loop throught each object 
    foreach ($keys as $key){ // loop through all the keys (fname, lname, email... 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.