Zend Framework Tutorial Connection database and perform additions and deletions of the method (with demo source download), zenddemo_php tutorial

Source: Internet
Author: User
Tags mysql host zend framework

Zend Framework Tutorial Connection database and perform additions and deletions of the method (with demo source download), Zenddemo


In this paper, we describe the connection database of Zend Framework tutorial and the method of adding and deleting. Share to everyone for your reference, as follows:

Let's start by creating a table called message in the database, which has three fields. Id,title,content. Where ID is the primary key.

Now let's start with the first step: Add a config folder under the Application folder and add a Config.ini file here. This is the configuration database basic information.

As shown in the following code:

[General]db.adapter=pdo_mysql//Please open PDO extension db.config.host=localhost//mysql host Db.config.username=root// Username db.config.password=//password, I here is empty db.config.dbname=zendoophp//database name

Step Two: Add a message.php file under the Models folder under Application. The name here is the same as the data table.

<?phpclass Message extends Zend_db_table {protected $_name = "Message";p rotected $_primary = ' id ';}

Step three: Next. We want to add the following code to our portal file index.php:

Configure database parameters and connect to database $config =new zend_config_ini ('./application/config/config.ini ', NULL, true); Zend_registry::set (' config ', $config); $dbAdapter =zend_db::factory ($config->general->db->adapter, $config->general->db->config-> ToArray ()); $dbAdapter->query (' SET NAMES UTF8 '); Zend_db_table::setdefaultadapter ($dbAdapter); Zend_registry::set (' Dbadapter ', $dbAdapter);

Fourth step: We are going to operate on our indexcontroller.php controller. There are four methods respectively. Their role is to add data, modify,

Delete the data. The program is as follows: (I have annotations in the programmer. Don't say more here!):

Class Indexcontroller extends Zend_controller_action {function init () {$this->registry = Zend_registry::getinstan   CE ();   $this->view = $this->registry[' view ';  $this->view->baseurl = $this->_request->getbaseurl (); } function Indexaction () {$message =new message ();//Instantiate the database class//Assign a value to the variable here, display the $this in the index.phtml template->view->bodyti   tle = ' Hello world! ';   All data is taken. Two-dimensional array $this->view->messages= $message->fetchall ()->toarray ();   Print_r ($this->view->messages); echo $this->view->render (' index.phtml ');//Display template} function Addaction () {//If it is a post-over value. Otherwise it will display the Add page if ( Strtolower ($_server[' request_method ')) = = ' Post ') {//filter some data. But there are some things that have been detected here.//Please Add.  I don't write that much. Time relationship: Zend_loader::loadclass (' zend_filter_striptags ');  $filter =new zend_filter_striptags ();  $content = $filter->filter ($this->_request->getpost (' content '));  $title = $filter->filter ($this->_request->getpost (' title '));  $message =new message (); $data =aRray (' content ' = $content, ' title ' = $title);  $message->insert ($data);  Unset ($data); Echo ' You add data to success! Please $this->view->baseurl. '   /index/index/"> Back"; }else{echo $this->view->render (' add.phtml ');//Show Add template}} public Function Editaction () {$message =new message  ();  $db = $message->getadapter ();  Zend_loader::loadclass (' zend_filter_striptags ');  $filter =new zend_filter_striptags (); Same as above Addaction if (Strtolower ($_server[' request_method ') = = ' Post ') {$content = $filter->filter (($this->_  Request->getpost (' content '));  $title = $filter->filter ($this->_request->getpost (' title '));   $id = $filter->filter (($this->_request->getpost (' id ')));  $set =array (' content ' = $content, ' title ' and ' $title ');  $where = $db->quoteinto (' id =? ', $id);  Update table Data $message->update ($set, $where) unset ($set); Echo ' You modify the data successfully! Please $this->view->baseurl. '  /index/index/"> Back"; }else{$id = $filter->filter ($this->_request->getparam(' ID ')));   $this->view->messages= $message->fetchall (' id= ' $id)->toarray ();  echo $this->view->render (' edit.phtml ');//display Edit Template}} public Function Delaction () {$message =new message ();   Can delete data by ID. There are some actions that are not done here. For example, there is no ID page where to go. . I'm just giving you a thought.  So not so complete $id = (int) $this->_request->getparam (' id ');    if ($id > 0) {$where = ' id = '. $id;  $message->delete ($where); } echo ' you deleted data successfully! Please $this->view->baseurl. '  /index/index/"> Back"; } }

The fifth step is to add the corresponding view. That is, the page template. They are add.phtml,edit.phtml,index.phtml. This is also annotated in the program. Please download the file to run the view.

Full instance code click here to download this site.

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

Articles you may be interested in:

    • Zend Framework Framework Tutorial Zend_db_table_rowset Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table_row Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table Usage
    • Zend Framework Tutorial Zend_form component implement form submission and display Error prompt method
    • Introduction to Zend Framework Development Classic Tutorial
    • Zend Framework Smarty Extension Implementation method
    • Code analysis of routing mechanism of Zend framework framework
    • Zend Framework implementation of basic functions of the message book (with demo source download)
    • The Zend framework implements the method of storing the session in Memcache
    • Zend Framework Paging Class usage
    • Zend Framework implements multi-file upload function instances
    • Zend Framework Introduction Environment configuration and the first Hello World example (with demo source download)
    • Zend Framework Tutorial Zend_db_table Table Association Instance detailed

http://www.bkjia.com/PHPjc/1113739.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113739.html techarticle Zend Framework Tutorial Connection database and perform additions and deletions of the method (with demo source download), Zenddemo This article describes the Zend Framework tutorial connection database and perform additions and deletions of the method ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.