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.