• 注册
当前位置:1313e > php >正文

php获取json文件数据并动态修改网站头部文件meta信息 --基于CI框架

话不多说了。直接开始吧  (如果有中文。请注意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

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值判断返回结果
}

}

?>

如果上边的看不明白的话。那就看这个,二者等价,不过这个比较麻烦。如果修改的话还得改这个类库

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);

 

最后看视图



 

原创作品。。仅供学习之用,

 

 

转载于:https://www.cnblogs.com/code_fbi/p/4146857.html

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录
相关推荐