PHP Server Development App Client Homepage Interface Development (i) Overview and scenario One: Read the database mode development Home interface

Source: Internet
Author: User
Tags php server

The main content of this post: 1. Brief introduction of three kinds of programs for the development of the App Client homepage interface

2. Example Tutorial : Read the database mode development home Interface


/************************************************************************************************************** ********************/

Scheme one: Read the database mode development Home interface

1. Getting messages from the database

2. Encapsulating data
3. Generating interface Data
Applicable scenarios:

System with high data timeliness


Scenario Two: Read the cache mode development Home interface


1. Getting messages from the database
2. Encapsulating data and writing to cache (Memcache mode)
3. Generating interface Data




Application Scenario: Reduce database pressure


Scenario Three: Timed read cache mode development Home interface
Using Crontab timed Tasks




Plan a detailed: Read the database mode development Home interface
Focus: 1. How to get Data 2. How to generate communication data
Analyze Some Apps home page
MU Lesson Net: The homepage is the list page shows the course information, not a full page display, but partially loaded. Load by accessing the database.
Sohu Video HD: A lot of modules, but the principle and Mu-class network similar. is to get the data from the database.


Development process:
1. Requesting a server via HTTP
2. Server query data

3. The server returns data



Instance:


list.php

<?php//This is the app home interface//access mode Http://****.com/list.php?page=1&pagesize=12require_once ('./response.php ');// Introduce the Data encapsulation class $page=isset ($_get[' page ')? $_get[' Page ']:1;//determines if the page's GET value is passed in, and if it is assigned to the page, the assignment is 1$pagesize=isset ($_get[' Pagesizze '])? $_get[' PageSize ']:1;//the same judgment pagesize//judging the legitimacy of incoming data (whether it is a number) if (!is_numeric ($page) | |! Is_numeric ($pageSize)) {//code is set to 401, which is specified by the service-side engineer and needs to be described in the interface documentation Response::show (401, ' data is not legal '); echo 1213123123123;} The starting value gets the PageSize bar data from offset start $offset= ($page-1) * $pageSize; $sql = "SELECT * FROM video where status= 1 order BY" Desc Li MIT ". $offset.", ". $pageSize; echo $sql;//Print SQL


Communication Data (Xml,json) encapsulation class response.php



<?phpclass Response {Const JSON = "JSON";/*** output communication data in an integrated manner * @param integer $code Status code * @param string $message message * @para M array $data data * @param string $type data type * return string*/public static function show ($code, $message = ', $data = array (), $type = Self::json) {if (!is_numeric ($code)) {return ';} $type = isset ($_get[' format ')?  $_get[' format ': self::json; $result = Array (' Code ' = $code, ' message ' = $message, ' data ' = $data,); if ($type = = ' json ') {Self::json ($code, $message, $data); exit;} elseif ($type = = ' array ') {var_dump ($result);} elseif ($type = = ' xml ') { Self::xmlencode ($code, $message, $data); exit;} else {//todo}}/*** output Communication data as JSON * @param integer $code Status code * @param string $message message * @param array $data data * Return St Ring*/public static function json ($code, $message = ", $data = Array ()) {if (!is_numeric ($code)) {return ';} $result = Array (' Code ' = $code, ' message ' = $message, ' data ' = $data); echo Json_encode ($result); exit;} /*** output Communication data as XML * @param inteGER $code Status Code * @param string $message message * @param array $data data * return string*/public static function Xmlencode ($code, $message, $data = Array ()) {if (!is_numeric ($code)) {return ';} $result = Array (' Code ' = $code, ' message ' = = $message, ' data ' = $data,); Header ("Content-type:text/xml"); $xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n"; $xml. = "<root>\n"; $xml. = Self::xmltoencode ($result); $xml. = " </root> "; Echo $xml;} public static function Xmltoencode ($data) {$xml = $attr = ""; foreach ($data as $key + = $value) {if (Is_numeric ($key)) {$a TTR = "id= ' {$key} '"; $key = "Item";} $xml. = "<{$key} {$attr}>"; $xml. = Is_array ($value)? Self::xmltoencode ($value): $value; $xml. = "</{$key}>\n";} return $xml;}}

HTTP request list.php



Page Display Results


To be continued .....

PHP Server Development App Client Homepage Interface Development (i) Overview and scenario One: Read the database mode development Home interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.