攻克CakePHP系列二 表单数据显示

前端之家收集整理的这篇文章主要介绍了攻克CakePHP系列二 表单数据显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. CREATETABLE`companies`(
  2. `id`int(11)NOTNULLauto_increment,
  3. `company`varchar(50)NOTNULL,
  4. `price`decimal(8,2)NOTNULL,
  5. `change`decimal(8,
  6. `lastudp`dateNOTNULL,
  7. PRIMARYKEY(`id`)
  8. )ENGINE=MyISAMAUTO_INCREMENT=8DEFAULTCHARSET=utf8;
  9. ------------------------------
  10. --Records
  11. ------------------------------
  12. INSERTINTO`companies`VALUES('1','3mCo','71.72','0.02','2008-10-21');
  13. INSERTINTO`companies`VALUES('2','AlcoaInc','29.01','0.42','2008-10-20');
  14. INSERTINTO`companies`VALUES('3','AT&TInc.','31.61','-0.48','2008-10-21');
  15. INSERTINTO`companies`VALUES('4','BoeingCo.','75.43','0.53','2008-10-13');
  16. INSERTINTO`companies`VALUES('5','UnitedTechnologiesCorporation','63.26','0.55','2008-10-09');
  17. INSERTINTO`companies`VALUES('6','IntelCorporation','19.88','0.31','2008-10-15');
  18. INSERTINTO`companies`VALUES('7','ExxonMobilCorp','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. 'prefix'=>'');
  12. }

companies_controller.PHP

  1. PHP
  2. class

    CompaniesController

    extends

    AppController
  3. {
  4. var

    $name='Companies';
  5. function

    index()
  6. {
  7. $this->set('companies',$this->Company->findAll());
  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. PHP
  2. class

    Company

    extends

    AppModel
  3. {
  4. var

    $name='Company';
  5. }
  6. ?>

index.thtml:

  1. Testcompanies

  2. Id
  3. company
  4. price
  5. change
  6. lastupdate
  7. PHP

    foreach

    ($companies

    as

    $company):?>
  8. PHPecho$company['Company']['id'];?>
  9. PHPecho$html->link($company['Company']['company'],"/companies/view/".$company['Company']['id']);?>
  10. PHPecho$company['Company']['price'];?>
  11. PHPecho$company['Company']['change'];?>
  12. PHPecho$company['Company']['lastudp'];?>
  13. PHP

    endforeach

    ;?>

view.thtml:

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

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

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

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

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

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

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

原文链接:https://www.f2er.com/php/29416.html

猜你在找的PHP相关文章