当前位置:Gxlcms > PHP教程 > 攻克CakePHP系列二表单数据显示_PHP

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

时间:2021-07-01 10:21:17 帮助过:33人阅读

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

  1. CREATE TABLE `companies` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `company` varchar(50) NOT NULL,
  4. `price` decimal(8,2) NOT NULL,
  5. `change` decimal(8,2) NOT NULL,
  6. `lastudp` date NOT NULL,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
  9. -- ----------------------------
  10. -- Records
  11. -- ----------------------------
  12. INSERT INTO `companies` VALUES ('1', '3m Co', '71.72', '0.02', '2008-10-21');
  13. INSERT INTO `companies` VALUES ('2', 'Alcoa Inc', '29.01', '0.42', '2008-10-20');
  14. INSERT INTO `companies` VALUES ('3', 'AT&T Inc.', '31.61', '-0.48', '2008-10-21');
  15. INSERT INTO `companies` VALUES ('4', 'Boeing Co.', '75.43', '0.53', '2008-10-13');
  16. INSERT INTO `companies` VALUES ('5', 'United Technologies Corporation', '63.26', '0.55', '2008-10-09');
  17. INSERT INTO `companies` VALUES ('6', 'Intel Corporation', '19.88', '0.31', '2008-10-15');
  18. INSERT INTO `companies` VALUES ('7', 'Exxon Mobil Corp', '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. 'connect' => 'mysql_connect',
  12. 'host' => 'localhost',
  13. 'login' => 'root',
  14. 'password' => 'root',
  15. 'database' => 'cake_ext',
  16. 'prefix' => '');
  17. }

companies_controller.php:

  1. class CompaniesController extends AppController
  2. {
  3. var $name = 'Companies';
  4. function index()
  5. {
  6. $this->set('companies', $this->Company->findAll());
  7. }
  8. function view($id = null)
  9. {
  10. $this->Company->id = $id;
  11. $this->set('company', $this->Company->read());
  12. }
  13. }
  14. ?>

company.php:

  1. class Company extends AppModel
  2. {
  3. var $name = 'Company';
  4. }
  5. ?>

index.thtml:

  1. Test companies

  2. foreach ($companies as $company): ?>
  3. endforeach; ?>
  4. Id company price change last update
    $company['Company']['id']; ?>
  5. $html->link($company['Company']['company'], "/companies/view/".$company['Company']['id']); ?>
  6. $company['Company']['price']; ?> $company['Company']['change']; ?> $company['Company']['lastudp']; ?>

view.thtml:

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

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

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

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

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

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

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

人气教程排行