php伺服器開發之 app用戶端首頁介面開發(一) 概述及方案一:讀取資料庫方式開發首頁介面

來源:互聯網
上載者:User

標籤:des   style   blog   http   ar   io   使用   sp   for   

本篇博文主要內容:1.簡述app用戶端首頁介面開發的三種方案

                                 2.執行個體講解 方案一:讀取資料庫方式開發首頁介面


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

方案一:讀取資料庫方式開發首頁介面

1.從資料庫擷取訊息

2.封裝資料
3.產生介面資料
適用情境:

資料時效性高的系統


方案二:讀取緩衝方式開發首頁介面


1.從資料庫擷取訊息
2.封裝資料,並寫入緩衝(memcache方式)
3.產生介面資料


  



適用情境:減少資料庫壓力


方案三:定時讀取緩衝方式開發首頁介面
使用crontab定時任務




方案一詳解:讀取資料庫方式開發首頁介面
重點:1.如何擷取資料2.如何產生通訊資料
分析部分APP首頁
慕課網:首頁是列表頁顯示課程資訊,不是一頁全部顯示,而是部分載入。通過訪問資料庫載入。
搜狐視頻HD:模組很多,但是原理和慕課網類似。都是從資料庫擷取資料。


開發流程:
1.通過http請求伺服器
2.伺服器查詢資料

3.伺服器返回資料



執行個體:


list.php

<?php//這是app首頁介面//訪問方式http://****.com/list.php?page=1&pagesize=12require_once('./response.php');//引入資料封裝類$page=isset($_GET['page'])?$_GET['page']:1;//判斷是否傳入page的get值,如果傳入對page賦值,否則賦值為1$pageSize=isset($_GET['pageSizze'])?$_GET['pageSize']:1;//同理判斷pageSize//判斷傳入資料合法性(是否為數字)if(!is_numeric($page)||!is_numeric($pageSize)){//code設定為401,這是服務端工程師規定的,需要在介面文檔說明Response::show(401,'資料不合法');echo 1213123123123;}//起始值從offset開始擷取pagesize條資料$offset=($page-1)*$pageSize;$sql="select * from video where status= 1 order by orderby desc limit".$offset."," .$pageSize;echo $sql;//列印sql


通訊資料(xml,json)封裝類 response.php



<?phpclass Response {const JSON = "json";/*** 按綜合方式輸出通訊資料* @param integer $code 狀態代碼* @param string $message 提示資訊* @param array $data 資料* @param string $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}}/*** 按json方式輸出通訊資料* @param integer $code 狀態代碼* @param string $message 提示資訊* @param array $data 資料* return string*/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;}/*** 按xml方式輸出通訊資料* @param integer $code 狀態代碼* @param string $message 提示資訊* @param array $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)) {$attr = " id='{$key}'";$key = "item";}$xml .= "<{$key}{$attr}>";$xml .= is_array($value) ? self::xmlToEncode($value) : $value;$xml .= "</{$key}>\n";}return $xml;}}

http請求list.php



網頁顯示結果


未完待續。。。。。。。

 

php伺服器開發之 app用戶端首頁介面開發(一) 概述及方案一:讀取資料庫方式開發首頁介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.