I have recently created a simple demo for using the PHP Zend Framework. The simple demo was prepared in order to assist my students doing their first steps using this framework. The demo includes few controllers and it basically shows a list of books and a list of reviews for each one of them.

In order to execute this code sample make sure you are using the Zend Server community edition that already comes with the Zend Framework installed, download the code and extract it into the htdocs folder.

Create a new database with the name ‘library’ and create a user for accessing it. The username should be ‘iuser’ and the password should be ‘ipassword’. Once the user was created create the tales and populate them with sample data using the following SQL code:

DROP TABLE IF EXISTS books;
CREATE TABLE books (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR( 100 ) NOT NULL ,
author VARCHAR( 100 ) NULL ,
date_updated DATETIME NOT NULL
);

DROP TABLE IF EXISTS reviews;
CREATE TABLE reviews (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
date_created DATETIME NOT NULL ,
book_id INT NOT NULL ,
user_name VARCHAR(50) NOT NULL ,
body MEDIUMTEXT NOT NULL ,
rating INT NULL
);

TRUNCATE books;
TRUNCATE reviews;

INSERT INTO books (id,name,author,date_updated)
VALUES 
('1', 'Beginning PHP and MySQL: From Novice to Professional', 'W. Jason Gilmore','2008-03-24 00:00:00'),
('2', 'Programming PHP', 'Rasmus Lerdorf, Kevin Tatroe, and Peter MacIntyre','2006-04-28 00:00:00'),
('3', 'PHP for the World Wide Web, Third Edition', 'Larry Ullman','2008-12-22 00:00:00'),
('4', 'Head First PHP and MySQL', 'Lynn Beighley and Michael Morrison','2008-12-30 00:00:00'),
('5', 'PHP Cookbook', 'Adam Trachtenberg and David Sklar','2006-08-22 00:00:00'),
('6', 'Practical Web 2.0 Applications with PHP', 'Quentin Zervaas','2007-12-20 00:00:00'),
('7', 'PHP Objects, Patterns, and Practice, Second Edition', 'Matt Zandstra','2007-12-20 00:00:00'),
('8', 'Web Database Applications with PHP and MySQL', 'Hugh E. Williams and David Lane','2004-05-16 00:00:00')
;

INSERT INTO reviews (date_created, book_id, user_name, body, rating)
VALUES
('2009-02-24 00:00:00',8, 'michh', 'Great book about PHP... I recommend!', 5),
('2009-05-14 20:01:00',8, 'jane', 'An excellent book!', 4),
('2009-05-14 20:01:00',1, 'josh', 'Great purchase. I recommend on buying it!', 4),
('2009-05-14 20:01:00',1, 'cary', 'Great book for everyday student!', 5),
('2009-01-20 05:04:00',2, 'gogo23', 'I dont like this book... I recommend to avoid it!', 2)
;

In order to execute the code sample try to browse at http://localhost:10088/zfapp/index.php.

The demo include three controllers: Books, Error and Index. The Index controller is responsible for generating the main entrance page of the application. The Index controller includes one action only. The index action.

<?php

require_once ROOT_DIR.'/models/Books.php';
require_once ROOT_DIR.'/models/Reviews.php';

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->titlee = 'Welcome to My Favorite Books!';
		$this->view->developer = 'Haim Michael';
        $booksFinder = new Books();
        $this->view->books = $booksFinder->fetchLatest();
    }
}
?>

The view generated for the index action is defined using the index.phtml file, you can find inside views/scripts/index.

<h1><?php echo $this->escape($this->titlee);?></h1>
<p>Welcome to My Favorite Books Reviews Website! This web site provides more information about my favorite books!</p>

<?php if(count($this->books)) { ?>
<h2>Recent Books</h2>
<table>
<?php foreach($this->books as $book) { ?>
<tr>
    <td><a href="<?php echo $this->url(array('controller'=>'books', 'action'=>'index', 'id'=>$book->id)) ;?>"><?php echo $this->escape($book->name); ?></a></td>
    <td><?php echo $this->displayDate($book->date_updated); ?></td>
</tr>
<?php } ?>
</table>
<p>Developed by <?php echo $this->developer; ?>
<?php } ?>

The Books controller includes the index action. That action is responsible for generating detailed info about each book including its reviews.

<?php
require_once ROOT_DIR.'/models/Books.php';
require_once ROOT_DIR.'/models/Reviews.php';

class BooksController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $id = (int)$this->getRequest()->getParam('id');
        if ($id == 0) {
            $this->_redirect('/');
            return;
        }

        $booksFinder = new Books();
        $book = $booksFinder->fetchRow('id='.$id);
        if ($book->id != $id) {
            $this->_redirect('/');
            return;
        }
        $this->view->book = $book;
        $this->view->title = $book->name;

        $reviewsFinder = new Reviews();
        $this->view->reviews = $reviewsFinder->fetchByBookId($id);

    }
}

The view of the index action that belongs to the Books controller can be found at views/scripts/books/index.phtml.

<h1><?php echo $this->escape($this->title);?></h1>
<div class="book_title">
<?php echo nl2br($this->escape($this->title));?>
</div>

<h2>Reviews</h2>
<?php if(count($this->reviews)) : ?>
<ul id="reviews">
<?php foreach($this->reviews as $review) : ?>
<li>
    <p class="review-user_name">
        <?php echo $this->escape($review->user_name); ?>
    </p>
    <p class="review-body"><?php echo $this->escape($review->body); ?></p>
	<p class="review-date"><?php echo $this->displayDate($review->date_created); ?></p>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

The code that works with the database is located within the models folder. That folder includes two files: Books.php and Reviews.php. Each one of them is responsible for working with a specific table.

The Books.php file includes the definition for the Books class, that extends Zend_Db_Table. This class is responsible for working with the books table.

<?php
class Books extends Zend_Db_Table
{
    protected $_name = 'books';

    /**
     * Fetch the latest $count books
     *
     * @param int $count
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function fetchLatest($count = 10)
    {
        return $this->fetchAll(null, 'id DESC', $count);
    }
}
?>

The Reviews.php file includes the definition for the Reviews class, that extends Zend_Db_Table. This class is responsible for working with the reviews table.

<?php
class Reviews extends Zend_Db_Table
{
    protected $_name = 'reviews';

	public function fetchByBookId($book_id, $order = 'date_created DESC', $count=null)
	{
		$where = 'book_id = ' . (int)$book_id;
		$order = 'date_created DESC';
		return $this->fetchAll($where, $order, $count);
	}
}
?>

The following screen shot is of the screen we get when browsing the main page of the web application.

The following screen shot is of the screen we get when browsing the reviews of a specific book.

Simple Zend Framework Jump Start PRO

Leave a Reply

Your email address will not be published. Required fields are marked *