Table of Contents
Test companies
'Company']['company']?>" >Company: $company['Company']['company']?>
Home Backend Development PHP Tutorial 攻克CakePHP系列二 表单数据显示_PHP

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

Jun 01, 2016 pm 12:24 PM
cakephp Data Display form

首先建立数据库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.     
  5.     function index()
  6.     {
  7.         $this->set('companies'$this->Company->findAll());
  8.     }
  9.     
  10.     function view($id = null)
  11.     {
  12.         $this->Company->id = $id;
  13.         $this->set('company'$this->Company->read());
  14.     }
  15. }
  16. ?>

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

How to implement page jump after PHP form submission How to implement page jump after PHP form submission Aug 12, 2023 am 11:30 AM

How to implement page jump after PHP form submission [Introduction] In web development, form submission is a common functional requirement. After the user fills out the form and clicks the submit button, the form data usually needs to be sent to the server for processing, and the user is redirected to another page after processing. This article will introduce how to use PHP to implement page jump after form submission. [Step 1: HTML Form] First, we need to write a page containing a form in an HTML page so that users can fill in the data that needs to be submitted.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How to use JavaScript to realize the automatic prompt function of the input box content of the form? How to use JavaScript to realize the automatic prompt function of the input box content of the form? Oct 20, 2023 pm 04:01 PM

How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: <!DOCTYP

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

See all articles