0

I want to display the whole data present in database in front end. I could not get how to display it in front end. However I can display a single row with the help of id. The given below is what I did

<?php
    $test_id = 3;
    $model = Mage::getModel('test/test')->load($test_id);
    echo '<tr>';
    echo '<td>'.$model->gettest_id().'</td>';
    echo '<td>'.$model->gettitle().'</td>';
    echo '<td>'.$model->getfilename().'</td>';
    echo '<td>'.$model->getcontent().'</td>';
    echo '<td>'.$model->getstatus().'</td>';
    echo '<td>'.$model->getcreated_time().'</td>';
    echo '<td>'.$model->getupdate_time().'</td>';
    echo '</tr>';
 ?>

Can anyone suggest me how to display the whole data from database. Thanx..

2

Use below code:

<?php
    $collection= Mage::getModel('test/test')->getCollection();
    foreach($collection as $model){
      echo '<tr>';
      echo '<td>'.$model->gettest_id().'</td>';
      echo '<td>'.$model->gettitle().'</td>';
      echo '<td>'.$model->getfilename().'</td>';
      echo '<td>'.$model->getcontent().'</td>';
      echo '<td>'.$model->getstatus().'</td>';
      echo '<td>'.$model->getcreated_time().'</td>';
      echo '<td>'.$model->getupdate_time().'</td>';
      echo '</tr>';
   }
 ?>
  • its giving error bro. (failed to open stream: No such file or directory in /var/www/html/magento/lib/Varien/Autoload.php on line 94 ) – Birju Feb 14 '17 at 11:53
  • where you have placed code and do you have test model? – Prashant Valanda Feb 14 '17 at 12:03
  • I placed it here /var/www/html/magento/app/design/frontend/default/models/template/test – Birju Feb 14 '17 at 12:04
  • models is your theme name? – Prashant Valanda Feb 14 '17 at 12:08
  • yes it is my module name – Birju Feb 14 '17 at 12:09
4

use getCollection() method of Model.

  • can u brief it please.. – Birju Feb 14 '17 at 11:36
2

Use this code :

<?php
$model = Mage::getModel('test/test')->getCollection();
foreach($model as $data)
{
    echo '<tr>';
    echo '<td>'.$data->getTestId().'</td>';
    echo '<td>'.$data->getTitle().'</td>';
    echo '<td>'.$data->getFilename().'</td>';
    echo '<td>'.$data->getContent().'</td>';
    echo '<td>'.$data->getStatus().'</td>';
    echo '<td>'.$data->getCreatedTime().'</td>';
    echo '<td>'.$data->getUpdateTime().'</td>';
    echo '</tr>';
}
 ?>
  • giving error bro (failed to open stream: No such file or directory in /var/www/html/magento/lib/Varien/Autoload.php on line 94) – Birju Feb 14 '17 at 11:55
  • @Birju Where you are putting this code. Also is your previous code is working with id ? – Ashish Jagnani Feb 14 '17 at 11:58
  • here /var/www/html/magento/app/design/frontend/default/models/tem‌​plate/test models is my module name. And yes it is working with id – Birju Feb 14 '17 at 12:19
  • its working now, I don't have collection file. I created it. Thanks for help. – Birju Feb 14 '17 at 12:31
1

Use Collection model of that model

$collection->getCollection();

Then add to collection addFieldToFilter('*') for all fields values.* use for all fields value

$collection->getCollection()->addFieldToFilter('*') ;

Then use

$collection->getData() which give an array with result

1

You can execute direct select query as shown below:

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$table = $resource->getTableName('test/test'); //here you should use resource entity name. 
$query = "SELECT * FROM {$table}";
$rows = $readConnection->fetchAll($query); 
print_r($rows);
0

Example is here man.

$collection= Mage::getModel('test/test')->getCollection();
    // you can use this print_r command to check if you getting data in collection.
    print_r($collection);

    if(count($collection)>0){
     foreach($collection as $coll){
     // use PHP Magic functions to get values.
      echo $coll->getTitle(); 
     }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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