WEB开发网
开发学院WEB开发PHP 攻克CakePHP系列二 表单数据显示 阅读

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

 2008-12-16 16:23:29 来源:WEB开发网   
核心提示:Cakephp表单数据显示的方法步骤首先建立数据库cake_ext,并执行如下sql文:CREATE TABLE `companies` (`id` int(11) NOT NULL auto_increment,`company` varchar(50) NOT NULL,`PRice` decimal(8,2) N

Cakephp表单数据显示的方法步骤

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

CREATE TABLE `companies` (
`id` int(11) NOT NULL auto_increment,
`company` varchar(50) NOT NULL,
`PRice` decimal(8,2) NOT NULL,
`change` decimal(8,2) NOT NULL,
`lastudp` date NOT NULL,
PRIMARYKEY(`id`)
)ENGINE=MyISAMAUTO_INCREMENT=8DEFAULTCHARSET=utf8;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `companies` VALUES ('1', '3m Co', '71.72', '0.02', '2008-10-21');
INSERT INTO `companies` VALUES ('2', 'Alcoa Inc', '29.01', '0.42', '2008-10-20');
INSERT INTO `companies` VALUES ('3', 'AT&T Inc.', '31.61', '-0.48', '2008-10-21');
INSERT INTO `companies` VALUES ('4', 'Boeing Co.', '75.43', '0.53', '2008-10-13');
INSERT INTO `companies` VALUES ('5', 'United Technologies Corporation', '63.26', '0.55', '2008-10-09');
INSERT INTO `companies` VALUES ('6', 'Intel Corporation', '19.88', '0.31', '2008-10-15');
INSERT INTO `companies` VALUES ('7', 'Exxon Mobil Corp', '68.10', '-0.43', '2008-10-17');
如下图所示建立工程:

数据库配置文件如下:

classDATABASE_CONFIG
{
var$default=array('driver'=>'MySQL',
'connect'=>'mysql_connect',
'host'=>'localhost',
'login'=>'root',
'passWord'=>'root',
'database'=>'cake_ext',
'prefix'=>'');

var$test=array('driver'=>'mysql',
'connect'=>'mysql_connect',
'host'=>'localhost',
'login'=>'root',
'password'=>'root',
'database'=>'cake_ext',
'prefix'=>'');
}
companies_controller.php

<?php
classCompaniesControllerextendsAppController
{
var$name='Companies';

functionindex()
{
$this->set('companies',$this->Company->findAll());
}

functionview($id= null)
{
$this->Company->id =$id;
$this->set('company',$this->Company->read());
}
}
?>
company.php


<?php
classCompanyextendsAppModel
{
var$name='Company';
}
?>
index.thtml:

<h1>Test companies</h1>
<table>
<tr>
<th>Id</th>
<th>company</th>
<th>price</th>
<th>change</th>
<th>last update</th>
</tr>
<?phpforeach($companiesas$company): ?>
<tr>
<td><?phpecho$company['Company']['id']; ?></td>
<td>
<?phpecho$html->link($company['Company']['company'],"/companies/view/".$company['Company']['id']); ?>
</td>
<td><?phpecho$company['Company']['price']; ?></td>
<td><?phpecho$company['Company']['change']; ?></td>
<td><?phpecho$company['Company']['lastudp']; ?></td>
</tr>
<?phpendforeach; ?>
</table>
view.thtml:

<h1>Company: <?phpecho$company['Company']['company']?></h1>
<p><small>Id: <?phpecho$company['Company']['id']?></small></p>
<p>Price: <?phpecho$company['Company']['price']?></p>
<p>Change: <?phpecho$company['Company']['change']?></p>
<p>LastUpdate: <?phpecho$company['Company']['lastudp']?></p>
访问http://localhost/cakephp/companies即可运行测试程序。


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

Tags:攻克 CakePHP 系列

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接