Zend framework configuration operation database instance analysis

Source: Internet
Author: User
Tags php foreach wordpress database

After the zendframework project environment is set up, check the zend framework configuration operation database. The php tutorial is as follows:
Create a config. ini file under the application/configs File
The configuration information is as follows::
[General]
Db. adapter = PDO_MYSQL
Db. config. host = localhost/IParess
Db. config. username = username
Db. config. password = password
Db. config. dbname = databasename
2,
On the index. php page of The pulibc directory
/** Zend_Application */
Require_once 'zend/Application. php ';
Insert
// Set the datase config
Require_once 'zend/Config/Ini. php ';
Require_once 'zend/Registry. php ';
Require_once 'zend/Db. php ';
Require_once 'zend/Db/Table. php ';
$ Config = new Zend_Config_Ini ('../application/configs/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: setdefaadapter adapter ($ dbAdapter );
Zend_Registry: set ('dbadapter ', $ dbAdapter );
In this case, I will use my local wordpress database for testing and use the wp_posts table for testing:
First, model models creates Wp_posts.php

Copy codeThe Code is as follows: <? Php
Class Wp_posts extends Zend_Db_Table {
Protected $ _ name = 'wp _ posts ';
Protected $ _ primary = 'id ';
}
?>

Create IndexController. php under controllerCopy codeThe Code is as follows: <? Php
Require_once APPLICATION_PATH. '/models/Wp_posts.php ';
Class IndexController extends Zend_Controller_Action
{
Public function init ()
{
/* Initialize action controller here */
}
Public function indexAction ()
{
$ Con = new Wp_posts ();
$ Res = $ con-> fetchAll ()-> toArray ();
$ This-> view-> res = $ res;
$ This-> render ("index ");
}
}

Create a view in views/scripts/index/: index. phtmlCopy codeThe Code is as follows: <Head>
<Title> this is for test </title>
</Head>
<Body>
<Table>
<? Php foreach ($ this-> res as $ news) {?>
<Tr>
<Td> <? Php echo $ news ['id']?> </Td>
<Td> <? Php echo $ news ['Post _ title']?> </Td>
<Td> <? Php echo $ news ['Post _ date']?> </Td>
</Tr>
<? Php }?>
</Table>
</Body>
</Html>

OK. The browser displays:

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.