Php programming example using zendframework

Source: Internet
Author: User
: This article mainly introduces the use of zendframework programming examples in php. For more information about PHP tutorials, see. In this article, refer to Chapter 4 of php top-level framework zendframe development practices and complete implementation...

First download the css file used: http://download.csdn.net/download/unityoxb/4058802

Decompress the package and copy the default and common files to the public/skins directory;

1. used database file mysql. SQL

Create table if not exists 'core _ pages '('id' int (10) unsigned not null auto_increment comment 'Page unique ID', 'CID' int (10) unsigned not null default '0' comment' category ID', 'uid' int (10) unsigned not null default '0' comment 'user ID', 'title' varchar (255) not null comment 'Page title', 'body' text not null comment' content', 'status' tinyint (4) not null default '1' comment' publish ', 'createtime' int (11) not null default '0' comment' create page time', 'updatetime' int (11) not null default '0' comment' modify page time ', 'comment' tinyint (4) not null default '0' comment' page comment function', 'Start' tinyint (4) not null default '0' comment' page level ', 'Top' tinyint (4) not null default '0' comment' pin to the top ', primary key ('id') ENGINE = InnoDB default charset = utf8;

Open mysql and use source mysql. SQL to create the table structure.

2. configure the application. ini file (hahacom/applicaton/configs)

Configure zend Framework to connect to mysql

[Development: production] phpsetment. display_startup_errors = 1phpSettings. display_errors = 1resources. frontController. params. displayExceptions = 1resources. db. adapter = "PDO_MYSQL" resources. db. params. host = "localhost" resources. db. params. username = "root" resources. db. params. password = "root" resources. db. params. dbname = "test" -- this is the data name resources. db. isDefaultTableAdapter = "TRUE" resources. db. params. driver_options.1002 = "set names UTF8 ;"

3. public/index. php

// Define application environmentdefined ('application _ env') | define ('application _ env', (getenv ('application _ env ')? Getenv ('application _ env '):'Development'); // Modify it to the test environment

4. create an article to display the model (the model mainly stores data models similar to javabean, or retrieves data from the database and stores the data in the memory)

Run the command: zf create model page.

A models/Page. php file is automatically generated.

 getAdapter();      $select = $db->select();      /*if($where != null)       {          //$select->where(' star = ? ', $where);          //$sql = $db->quoteInto("select * from `core_pages` where `star`= ?", $where);          //$result = $db->query($sql);          $select->from('core_pages','*')->where('star = ?', $where)->limit(1);       }*/       $select->from('core_pages','*');       if(count($where)>0)       {          foreach($where as $key=>$value)             $select->where($key.' = ?',$value);       }      //$row = $result->fetch();      $row = $db->fetchAll($select);      if($row)      {         return $row;      }      else      {         echo "=================";         return null;      }   }   public function getPages($where = null)   {       $db = Zend_Db_Table::getDefaultAdapter();       if(is_numeric($where))       {           //$row = $db->find($where)->current();          $select = $db->select();          $select->from('core_pages','*');          $select->where('id = ?', $where);          $row = $db->fetchRow($select);       }       if(is_array($where) && count($where)>0)       {                    $select = $db->select();          $select->from('core_pages','*');          foreach($where as $key=>$value){              $select->where($key.'=?', $value);          }          $row = $db->fetchAll($select);       }      if($row)      {         return $row;      }      else      {         echo "=================";         return null;      }   }}?>

5. create a controller

Run the command: zf create controller news will automatically generate controllers/NewsController. php

 1, 'comment'=>1);        $newsStar = $modelPage->getPage($where);        //print_r($newsStar);        $this->view->News = $newsStar;        //$this->view->name = "hahaha";     }}

Run the following commands: zf create controller page and zf create action detail page.

Controllers/PageController. php is automatically generated.

 _request->getParam('id');        $modelPage = new Application_Model_Page($id);//if($modelPage == null)  //print_r('==============================');//print_r($id);//print_r($modelPage);        $page = $modelPage->getPages($id);        $this->view->page = $page;    }}

5. create a view file.

/Views/scripts/news/index. phtml

 News[0]['title']."";echo $this->News[0]['body'];//echo $this->name;if($this->News){   /*echo "
 
 
    "; // print_r($this->News); foreach($this->News as $val) { echo "
  • "."".$val['title'].""."
  • "; } echo "
"; */ echo "
    "; echo $this->partialLoop('row_pages.phtml', $this->News); echo "
";}?>

/Views/scripts/row_pages.phtml

  • Id;?> "> Title;?> Posting time: Createtime);?>

  • /Views/scripts/page/detail. phtml

     Page ['title']. ""; echo :". date ('Y-m-D', $ this-> page ['createtime']). ""; echo"
     "; Echo $ this-> page ['body'];?>

    Run:


    Click the link:


    Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

    The above describes the use of zendframework programming examples in php, including the content, hope to be helpful to friends who are interested in PHP tutorials.

    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.