phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Zend Framework Database: Creating List of Data

Tag: framework, zend, zend framework, database, list data, grid data   Category: PHP Framework
post: 12 Apr 2008 read: 2,291


Zend Framework Database Step By Step Tutorial - Part 5: Now, we create table of data that we have inputed. For this job, we create a methode named "listAction" within controller. Then, create view for that list.

Open "UserController.php" within application/controllers. Add following method:


  public function listAction()
  {
    $params = array('host'		=>'localhost',
	                'username'	=>'root',
					'password'  =>'admin',
					'dbname'	=>'zend'
	               );
	$DB = new Zend_Db_Adapter_Pdo_Mysql($params);
    
	$DB->setFetchMode(Zend_Db::FETCH_OBJ);
	
	$sql = "SELECT * FROM `user` ORDER BY user_name ASC";
	$result = $DB->fetchAssoc($sql);
	
    $this->view->assign('title','Member List');
	$this->view->assign('description','Below, our members:');
	$this->view->assign('datas',$result);		
  
  }

Then, create named "list.phtml" within application/views/scripts/user. Enter following code:


<? include "header.phtml"; ?>
  <h1><?=$this->escape($this->title);?></h1>
  <h2><?=$this->escape($this->description);?></h2>
  <a href="register">Register</a>
  <table border="1">
    <tr>
	  <th>ID</th>
	  <th>User Name</th>	  
	  <th>First Name</th>	  
	  <th>Last Name</th>	  
	  <th>Action</th>	  
	</tr>
  <? $datas = $this->datas;
     for($i = 1; $i<= count($datas);$i++){ ?>
    <tr>
	  <td><?=$datas[$i]['id']?></td>	
	  <td><?=$datas[$i]['user_name']?></td>
	  <td><?=$datas[$i]['first_name']?></td>	  
	  <td><?=$datas[$i]['last_name']?></td>	  
	  <td>
	    <a href="edit/id/<?=$datas[$i]['id']?>">Edit</a>
		|
	    <a href="del/id/<?=$datas[$i]['id']?>">Delete</a>		
	  </td>	  
	</tr> 
  <? } ?>
  </table>
  
<? include "footer.phtml"; ?>

Point your browser to http://localhost/test/zend/helloworld/ web_root/user/list



Series this article:
Zend Framework Database: Intro
Zend Framework Database: Creating Input Form
Zend Framework Database: Inputing Data to Database
Zend Framework Database: Inserting Expressions to a table
Zend Framework Database: Creating List of Data
Zend Framework Database: Creating Editing Form
Zend Framework Database: Updating Data
Zend Framework Database: Updating Data Use Update Query From Zend Framework
Zend Framework Database: Deleting Data
Zend Framework Database: Delete Query Style
Zend Framework Database: Summarizing Action Controller

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting