攻克CakePHP系列二 表单数据显示
2015-01-24信息快讯网
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,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=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');
如下图所示建立工程:
数据库配置文件如下:
- class DATABASE_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
- class CompaniesController extends 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 Company extends AppModel
- {
- 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>
- <?php foreach ($companies as $company): ?>
- <tr>
- <td><?php echo $company['Company']['id']; ?></td>
- <td>
- <?php echo $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
- </td>
- <td><?php echo $company['Company']['price']; ?></td>
- <td><?php echo $company['Company']['change']; ?></td>
- <td><?php echo $company['Company']['lastudp']; ?></td>
- </tr>
- <?php endforeach; ?>
- </table>
view.thtml:
- <h1>Company: <?php echo $company['Company']['company']?></h1>
- <p><small>Id: <?php echo $company['Company']['id']?></small></p>
- <p>Price: <?php echo $company['Company']['price']?></p>
- <p>Change: <?php echo $company['Company']['change']?></p>
- <p>LastUpdate: <?php echo $company['Company']['lastudp']?></p>
访问http://localhost/cakephp/companies即可运行测试程序。
本代码参考自官方自带例子:http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial
Google Voice 短信发送接口PHP开源版(2010.5更新)
PHP 飞信好友免费短信API接口开源版
PHP计划任务之关闭浏览器后仍然继续执行的函数
PHP垃圾回收机制简单说明
PHP多线程抓取网页实现代码
Discuz!下Memcache缓存实现方法
memcached 和 mysql 主从环境下php开发代码详解
AMFPHP php远程调用(RPC, Remote Procedure Call)工具 快速入门教程
Windows下安装Memcached的步骤说明
来自phpguru得Php Cache类源码
PHP Socket 编程
Cakephp 执行主要流程
PHP CKEditor 上传图片实现代码
初学CAKEPHP 基础教程
php win下Socket方式发邮件类
配置Apache2.2+PHP5+CakePHP1.2+MySQL5运行环境
攻克CakePHP系列三 表单数据增删改
CakePHP去除默认显示的标题及图标的方法
攻克CakePHP系列一 连接MySQL数据库
攻克CakePHP(PHP中的Ruby On Rails框架)图文介绍第1/2页
PHP 反射机制实现动态代理的代码
PHP 面向对象改进后的一点说明第1/2页
Cannot modify header information错误解决方法
使用PHP socke 向指定页面提交数据
php socket方式提交的post详解
php出现Cannot modify header information问题的解决方法大全