To display the form data of CakePHP, first create the database cake_ext and execute the following SQL:
- Create table 'companys '(
- 'Id' int (11) not null auto_increment,
- 'Company' varchar (50) not null,
- 'Price' decimal (8, 2) not null,
- 'Change' decimal (8, 2) not null,
- 'Lastup' date not null,
- Primary key ('id ')
- )ENGINE=MyISAM AUTO_INCREMENT=8DEFAULTCHARSET=Utf8;
- ------------------------------
- -- Records
- ------------------------------
- Insert into 'companys' VALUES ('1', '3 m co', '71. 72 ', '0. 02', '2017-10-21 ');
- Insert into 'companys' VALUES ('2', 'Alcoa Inc', '29. 01', '0. 42', '2017-10-20 ');
- Insert into 'companys' VALUES ('3', 'AT & T Inc. ', '31. 61', '-0.48', '2017-10-21 ');
- Insert into 'companys' VALUES ('4', 'Boeing Co. ', '75. 43', '0. 53', '2017-10-13 ');
- Insert into 'companys' VALUES ('5', 'United Technologies Corporation ', '63. 26', '0. 55', '2017-10-09 ');
- Insert into 'companys' VALUES ('6', 'Intel Corporation ', '19. 88', '0. 31', '2017-10-15 ');
- Insert into 'companys' VALUES ('7', 'Exxon Mobil Corp ', '68. 10','-100', '2017-10-17 ');
Create a project as shown in:
The database configuration file is as follows:
- 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:
-
- ClassCompaniesControllerExtendsAppController
- {
- Var $ Name='Companys';
-
- FunctionIndex ()
- {
- $ This-> Set ('Companys',$ This-> Company-> findAll ());
- }
-
- FunctionView ($ Id= Null)
- {
- $ This-> Company-> id =$ Id;
- $ This-> Set ('Company',$ This-> Company-> read ());
- }
- }
- ?>
Company. php:
-
- ClassCompanyExtendsAppModel
- {
- Var $ Name='Company';
- }
- ?>
Index. thtml:
- Test companies
- Foreach($ Companies As $ Company):?>
- Endforeach;?>
| Id |
Company |
Price |
Change |
Last update |
| Echo $ Company['Company'] ['Id'];?> |
- Echo $ Html-> Link ($ Company['Company'] ['Company'],"/Companies/view /".$ Company['Company'] ['Id']);?>
|
Echo $ Company['Company'] ['Price'];?> |
Echo $ Company['Company'] ['Change'];?> |
Echo $ Company['Company'] ['Lastup'];?> |
View. thtml:
- Company: Echo $ Company['Company'] ['Company']?>
-
Id: Echo $ Company['Company'] ['Id']?>
-
Price: Echo $ Company['Company'] ['Price']?>
-
Change: Echo $ Company['Company'] ['Change']?>
-
LastUpdate: Echo $ Company['Company'] ['Lastup']?>
Access http: // localhost/cakephp/companies to run the test program.
This code reference from the official example: http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial