- CREATETABLE`companies`(
- `id`int(11)NOTNULLauto_increment,
- `company`varchar(50)NOTNULL,
- `price`decimal(8,2)NOTNULL,
- `change`decimal(8,
- `lastudp`dateNOTNULL,
- PRIMARYKEY(`id`)
- )ENGINE=MyISAMAUTO_INCREMENT=8DEFAULTCHARSET=utf8;
- ------------------------------
- --Records
- ------------------------------
- INSERTINTO`companies`VALUES('1','3mCo','71.72','0.02','2008-10-21');
- INSERTINTO`companies`VALUES('2','AlcoaInc','29.01','0.42','2008-10-20');
- INSERTINTO`companies`VALUES('3','AT&TInc.','31.61','-0.48','2008-10-21');
- INSERTINTO`companies`VALUES('4','BoeingCo.','75.43','0.53','2008-10-13');
- INSERTINTO`companies`VALUES('5','UnitedTechnologiesCorporation','63.26','0.55','2008-10-09');
- INSERTINTO`companies`VALUES('6','IntelCorporation','19.88','0.31','2008-10-15');
- INSERTINTO`companies`VALUES('7','ExxonMobilCorp','68.10','-0.43','2008-10-17');
如下图所示建立工程:
companies_controller.PHP:
- PHP
class
CompaniesControllerextends
AppController- {
var
$name='Companies';function
index()- {
- $this->set('companies',$this->Company->findAll());
- }
function
view($id=null)- {
- $this->Company->id=$id;
- $this->set('company',$this->Company->read());
- }
- }
- ?>
company.PHP:
- PHP
class
Companyextends
AppModel- {
var
$name='Company';- }
- ?>
index.thtml:
Testcompanies
Id company price change lastupdate - PHP
foreach
($companiesas
$company):?>PHPecho$company['Company']['id'];?> - PHPecho$html->link($company['Company']['company'],"/companies/view/".$company['Company']['id']);?>
PHPecho$company['Company']['price'];?> PHPecho$company['Company']['change'];?> PHPecho$company['Company']['lastudp'];?> - PHP
endforeach
;?>
view.thtml:
访问http://localhost/cakePHP/companies即可运行测试程序。
本代码参考自官方自带例子:http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial
原文链接:https://www.f2er.com/php/29416.html