Apply for Zend Framework Certification Training

Zend Framework 2




List of mysql data usng php  in zend framework 2
Step 1 
Create an Model file
<?php
namespace Application\Model;
use Zend\Db\TableGateway\TableGateway;

class MovieTable
{
    protected $tableGateway;
    
    public function __construct(TableGateway $tableGateway) 
    {
        $this->tableGateway = $tableGateway;
    }
    public function GetallData()
    {
        $result = $this->tableGateway->select();
        return $result;
    }
}

Step 2  create A Controller 

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\MovieTable;

class MovieController extends AbstractActionController
{
    protected $movieTable;
    public function getTabledata()
    {
        if(!$this->movieTable)
        {
            $sm = $this->getServiceLocator();
            $this->movieTable = $sm->get('Application\Model\MovieTable');
        }
        return $this->movieTable;
    }
    public function indexAction()
    {
        return new ViewModel(array('first_name'=>"Rajesh"));
    }
    public function listmoviedataAction()
    {
        return new ViewModel(array('listMovies'=>$this->getTabledata()->GetallData()));
    }
}

?>

Step 3 Add code in Module.php 

public function getServiceConfig()
     {
         return array(
             'factories' => array(
                 'Application\Model\MobileTable' =>  function($sm) {
                     $tableGateway = $sm->get('MobileTableGateway');
                     $table = new MobileTable($tableGateway);
                     return $table;
                 },
                 'MobileTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $resultSetPrototype = new ResultSet();
                    // $resultSetPrototype->setArrayObjectPrototype(new Mobile());
                     return new TableGateway('employee', $dbAdapter, null);
                 },
                'Application\Model\MovieTable' =>  function($sm) {
                     $tableGateway = $sm->get('MovieTableGateway');
                     $table = new MovieTable($tableGateway);
                     return $table;
                 },
                'MovieTableGateway' => function ($sm) {
                     $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                     $resultSetPrototype = new ResultSet();
                    // $resultSetPrototype->setArrayObjectPrototype(new Mobile());
                     return new TableGateway('users', $dbAdapter, null);
                 },
             ),
         );
     }

Step 4 create View File

<table class="table">
 <tr>
     <th>Title</th>
     <th>Artist</th>
     <th> </th>
 </tr>
 <?php foreach ($listMovies as $album) : ?>
 <tr>
     <td><?php echo $this->escapeHtml($album->name);?></td>
     <td><?php echo $this->escapeHtml($album->email);?></td>
     <td>
         <a href="<?php echo $this->url('application',
             array('action'=>'edit', 'uid' => $album->uid));?>">Edit</a>
         <a href="<?php echo $this->url('application',
             array('action'=>'delete', 'uid' => $album->uid));?>">Delete</a>
     </td>
 </tr>
 <?php endforeach; ?>
 </table>
                                    

< First Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top