Magento – how can we display data from database for all field form custom table in magento

magento-1.7

i have created one table which is name with example which have filed like firstname, last name, email. now i display data through

\app\design\frontend\default\example_theme\layout\nextbits_example.xml

<?xml version="1.0" encoding="UTF-8"?>


<layout version="0.1.0">

<example_index_index>
    <reference name="content">
        <block type="example/example" template="example/email2.phtml"/>
        <block type="example/example" template="catalog/nextbits_example.phtml"/>
    </reference>
    </example_index_index>

</layout>

\app\design\frontend\default\example_theme\template\catalog\nextbits_example.phtml

<div>
    <?php
        echo $this->getContent(); //disp emaill call method from block example
    ?>
</div>

\app\code\local\Xyz\Example\Block\Example.php

<?php
class NextBits_Example_Block_Example extends Mage_Core_Block_Template
{
    public function getContent()
    {
        $model = Mage::getModel('example/example')->load('1');
        $data = $model->getData();


        $model = Mage::getModel('example/example')->getCollection();

        echo '<pre>';


        foreach($model as $obj){
            print_R($obj->getData('fname'));
            echo "<br>";
        }  
    }

}

its output is only for one field suppose i get either email, or first name or lastname ,
Now if i want to display result of all the field , How can i get result of all field?

and also want only list of result filed wise like all id, then all the firstname and then all lastname like this..then what to do?

Thank You.!!!

Best Answer

Try this:

$collection = Mage::getModel('example/example')->getCollection();
$keys = array_keys($collection->getFirstItem()->getData());

now you have the you need.

foreach ($keys as $key){ // loop through all the keys (fname, lname, email... 
    foreach ($collection as $obj){//loop throught each object 
        print_r($obj->getData($key));//get the value for a speficic key. 
    } 
}