11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 24 • INTRODUCING THE ZEND FRAMEWORKwww.it-ebooks.infoThis example demonstrates how a new row can be added to the database without writing a singleline of SQL! I must stress that this example is only intended to show you the most direct approach toworking with the database; in a real-world situation you are encouraged to create a new model formanaging contacts and move the insertion syntax into that model, thereby hiding the functionality fromthe controller. Consult the Zend Framework documentation for all the details.Listing ContactsWith the ability to add contacts in place, you can create an interface for viewing the contact list. This ismuch less involved than the process used to add a contact, involving querying the database for a list ofcontacts, and then outputting those contacts within the view. To begin, modify the Contacts controller’sindex action to look like this:public function indexAction(){$contact = new Zend_Db_Table('contacts');$query = $contact->select()->order('name');$this->view->contacts = $contact->fetchAll($query);}This snippet will retrieve all records from the contacts table, ordering the results by the namecolumn. These results, which are returned from the fetchAll() method as an array of objects, areassigned to a variable named $contacts which resides in the view scope.Next, within the index.phtml view, add the following text:This results in the form shown in Figure 24-3.Figure 24-3. Viewing contactsI’ve covered a tremendous amount of ground in this chapter, not only introducing the concept of aweb framework but actually showing you how to install and configure the Zend Framework as well ascreate a simple application which demonstrates the framework’s key features. Yet there are dozens, ifnot hundreds, of other powerful features which are well worth your time to investigate. Be sure toconsult the Zend Framework documentation to learn more about what’s possible.474

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!