標籤:des style http io ar sp for on 檔案
話不多說了。直接開始吧 (如果有中文。請注意json只認utf-8編碼)
首先你需要有一個json檔案資料
{
"index": {
"title": "indexmytitle",
"keywords": "中文",
"content": "中文",
"description": "中文"
},
"goods": {
"title": "goodsmytitle",
"keywords": "goodskeywords",
"content": "goodsmycontent",
"description": "goodsmydes"
},
"shop": {
"title": "shopmytitle",
"keywords": "shopkeywords",
"content": "shopmycontent",
"description": "shopmudes",
"description1": "shopmudes"
}
}
然後呢。你需要在CI裡邊建一個公用類,,appliction/librarys目錄 Json.php
<?php if (!defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);
class Json {
public function some_function($seo_category){
$path = ‘http://127.0.0.1/項目名/public/json.json‘; //檔案路徑
$json_result= file_get_contents($path); //把json檔案讀入一個字串。
$json_array= json_decode($json_result,true); // json_decode對 JSON 格式的字串進行編碼,轉換成數組形式
return $json_array[$seo_category]; //根據key值判斷返回結果
}
}
?>
如果上邊的看不明白的話。那就看這個,二者等價,不過這個比較麻煩。如果修改的話還有改這個類庫
<?php if (!defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);
class Json {
public function some_function($seo_category){
$path = ‘http://127.0.0.1/ttzkq/public/json.json‘; //檔案路徑
$json_result= file_get_contents($path); //把json檔案讀入一個字串。
$json_array= json_decode($json_result,true); // json_decode對 JSON 格式的字串進行編碼,轉換成數組形式
//二維數組轉一維數組
foreach($json_array as $k => $v){
$$k = $v;
}
if($seo_category == ‘index‘){
return $index;
}elseif($seo_category == ‘goods‘){
return $goods;
}elseif($seo_category == ‘shop‘){
return $shop;
}
}
}
?>
然後再看控制器裡邊,,
public function __construct(){
$this->load->library(‘json‘); //載入json資料類庫
}
下邊方法裡邊調用,並傳值到html視圖裡邊
$top[‘json_result‘]= $this->json->some_function(‘index‘);
$this->load->view("templates/top",$top);
最後看視圖
<meta name=‘keywords‘ content="<?php echo $json_result[‘keywords‘];?>" >
<meta name=‘content‘ content="<?php echo $json_result[‘content‘];?>" >
<meta name=‘description‘ content="<?php echo $json_result[‘description‘];?>">
原創作品。。僅供學習之用,
php擷取json檔案資料並動態修改網站頭部檔案meta資訊 --基於CI架構