欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

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

程序员文章站 2022-06-05 17:46:26
...
首先建立数据库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. ?>