1

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.!!!

10
  • Try $obj->getData(), without any parameters.
    – Marius
    Commented Jul 7, 2013 at 18:09
  • @Marius no it not works.
    – MageDev
    Commented Jul 7, 2013 at 18:11
  • @Marius it shows like as below Array ( [example_id] => 1 [fname] => fname [lname] => lname [email] => [email protected] [date] => )
    – MageDev
    Commented Jul 7, 2013 at 18:16
  • wasn't that your question? you wanted all the fields, right?
    – Marius
    Commented Jul 7, 2013 at 18:18
  • @Marius ya its true. but, is this the only way to display all field? Can't we display any other way?
    – MageDev
    Commented Jul 7, 2013 at 18:23

1 Answer 1

2

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. 
    } 
}
1
  • A note here: This line $keys = array_keys($collection->getFirstItem()->getData()); only works when there is at least 1 existing row in table
    – 0xh8h
    Commented Jun 26, 2017 at 2:18

Your Answer

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

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