当前位置:首页 > PHP教程 > PHP高级教程

攻克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



    【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!