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:
-
- <? 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:
- <? PHP
- ClassCompanyExtendsAppmodel
- {
- VaR $ Name='Company';
- }
- ?>
Index. thtml:
-
- <H1> test companies
-
- <Table>
-
- <Tr>
-
- <TH> id </Th>
-
- <TH> company </Th>
- <TH> price </Th>
-
- <TH> change </Th>
-
- <TH> last update </Th>
-
- </Tr>
-
- <? PHPForeach($ Companies As $ Company):?>
-
- <Tr>
-
- <TD> <? PHPEcho $ Company['Company'] ['Id'];?> </TD>
-
- <TD>
- <? PHPEcho $ Html-> Link ($ Company['Company'] ['Company'],"/Companies/View /".$ Company['Company'] ['Id']);?>
-
- </TD>
-
- <TD> <? PHPEcho $ Company['Company'] ['Price'];?> </TD>
- <TD> <? PHPEcho $ Company['Company'] ['Change'];?> </TD>
-
- <TD> <? PHPEcho $ Company['Company'] ['Lastup'];?> </TD>
-
- </Tr>
-
- <? PHPEndforeach;?>
-
- </Table>
View. thtml:
- <H1> company: <? PHPEcho $ Company['Company'] ['Company']?> </H1>
-
- <P> <small> ID: <? PHPEcho $ Company['Company'] ['Id']?> </Small> </P>
-
- <P> price: <? PHPEcho $ Company['Company'] ['Price']?> </P>
- <P> change: <? PHPEcho $ Company['Company'] ['Change']?> </P>
-
- <P> lastupdate: <? PHPEcho $ Company['Company'] ['Lastup']?> </P>
Access http: // localhost/CakePHP/companies to run the test.Program.
BenCodeReference from the official example: http://book.cakephp.org/view/326/The-Cake-Blog-Tutorial