Home > Backend Development > PHP Tutorial > Overcoming CakePHP Series 2 Form Data Display_PHP Tutorial

Overcoming CakePHP Series 2 Form Data Display_PHP Tutorial

WBOY
Release: 2016-07-21 15:49:09
Original
1039 people have browsed it

首先建立数据库cake_ext,并执行如下sql文:

  1. CREATE TABLE `companies` (
  2.   `id` int(11) NOT NULL auto_increment,
  3.   `company` varchar(50) NOT NULL,
  4.   `price` decimal(8,2) NOT NULL,
  5.   `change` decimal(8,2) NOT NULL,
  6.   `lastudp` date NOT NULL,
  7.   PRIMARY KEY  (`id`)
  8. ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
  9. -- ----------------------------
  10. -- Records 
  11. -- ----------------------------
  12. INSERT INTO `companies` VALUES ('1', '3m Co', '71.72', '0.02', '2008-10-21');
  13. INSERT INTO `companies` VALUES ('2', 'Alcoa Inc', '29.01', '0.42', '2008-10-20');
  14. INSERT INTO `companies` VALUES ('3', 'AT&T Inc.', '31.61', '-0.48', '2008-10-21');
  15. INSERT INTO `companies` VALUES ('4', 'Boeing Co.', '75.43', '0.53', '2008-10-13');
  16. INSERT INTO `companies` VALUES ('5', 'United Technologies Corporation', '63.26', '0.55', '2008-10-09');
  17. INSERT INTO `companies` VALUES ('6', 'Intel Corporation', '19.88', '0.31', '2008-10-15');
  18. INSERT INTO `companies` VALUES ('7', 'Exxon Mobil Corp', '68.10', '-0.43', '2008-10-17');

如下图所示建立工程:

数据库配置文件如下:

  1. class DATABASE_CONFIG
  2. {
  3.     var $default = array('driver' => 'mysql',
  4.                                 'connect' => 'mysql_connect',
  5.                                 'host' => 'localhost',
  6.                                 'login' => 'root',
  7.                                 'password' => 'root',
  8.                                 'database' => 'cake_ext',
  9.                                 'prefix' => '');
  10.     var $test = array('driver' => 'mysql',
  11.                             'connect' => 'mysql_connect',
  12.                             'host' => 'localhost',
  13.                             'login' => 'root',
  14.                             'password' => 'root',
  15.                             'database' => 'cake_ext',
  16.                             'prefix' => '');
  17. }

companies_controller.php:

  1. class CompaniesController extends AppController
  2. {
  3. var $name = 'Companies';
  4. function index()
  5. {
  6. $this->set('companies'$this->Company->findAll());
  7.     }
  8.     
  9.     function view($id = null)
  10.     {
  11.         $this->Company->id = $id;
  12.         $this->set('company'$this->Company->read());
  13.     }
  14. }
  15. ?>

company.php:

 

  1. class Company extends AppModel
  2. {
  3. var $name = 'Company';
  4. }
  5. ?>

index.thtml:

  1. Test companies

  2. foreach ($companies as $company): ?>
  3. endforeach; ?>  
  4. Id company price change last update
    echo $company['Company']['id']; ?>
  5. echo $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
  6. echo $company['Company']['price']; ?> echo $company['Company']['change']; ?> echo $company['Company']['lastudp']; ?>

view.thtml:

  1. Company: echo $company['Company']['company']?>

  2. Id: echo $company['Company']['id']?>

  3. Price: echo $company['Company']['price']?>

  4. Change: echo $company['Company']['change']?>

  5. LastUpdate: echo $company['Company']['lastudp']?>

访问http://localhost/cakephp/companies即可运行测试程序。

 

本代码参考自官方自带例子:http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/319623.htmlTechArticle首先建立数据库cake_ext,并执行如下sql文: CREATETABLE`companies`( `id`int(11)NOTNULLauto_increment, `company`varchar(50)NOTNULL, `price`decimal(8,2)NOTNULL, `change`...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template