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

  1. jQuery获取JSON格式数据方法

    getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ ...

  2. js 获取input type="file" 选择的文件大小、文件名称、上次修改时间、类型等信息

    文件名的传递 ---全路径获取 $('#file').change(function(){ $('#em').text($('#file').val()); }); 文件名的传递 ---只获取文件名 ...

  3. 以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。

    MainWindow.xaml文件 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...

  4. jQuery中使用Ajax获取JSON格式数据示例代码

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于“名称”和“值”的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中 ...

  5. qt qml ajax 获取 json 天气数据示例

    依赖ajax.js类库,以下代码很简单的实现了获取天气json数据并展示的任务 [TestAjax.qml] import QtQuick 2.0 import "ajax.js" ...

  6. Jmeter4.0 _Beanshell解析并获取json响应数据数组长度

    我们在做jmeter接口测试的时候,有时候碰到开发没返回数据total,只返回了一条条记录,可是呢,我们又需要知道到底返回了多少条数据时,咋办呢?咋办呢?咋办呢? 不要急,接下来,让我们见证奇迹是如何 ...

  7. 专家动态页面的实现——php基于CI框架的学习(二)

    以下是本次学习的页面 打开相关文件,整个定义了一个Expert类 class Expert extends CI_Controller{} 在Expert类里定义了几个参数以及说明其使用了哪些mode ...

  8. ASP.NET 跨域获取JSON天气数据

    前几天做一个门户网站,在首页需要加载气象数据,采用了中央气象局的接口. 刚开始采用JSONP在前台跨域请求数据,没成功~ 后换成在c#后台请求数据返回... 前端代码: $(function () { ...

  9. jQuery调用ajax获取json格式数据

    <body> <div>点击按钮获取音乐列表</div> <input type="button" id="button&quo ...

随机推荐

  1. css 正方体

    <!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...

  2. Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)

    Applet程序. 定义自由曲线的类有两个,其中一个定义二次曲线,另一个定义三次曲线.这些自由曲线是用一系列线段定义的参数化曲线.二次曲线段用方程定义,方程包含独立变量x的平方.三次曲线也用方程定义, ...

  3. 异常积累:org.hibernate.StaleStateException

    ERROR - Exception executing batch:  org.hibernate.StaleStateException: Batch update returned unexpec ...

  4. c++怎么将一个类,拆分出接口类,和实现类

    还拿上一遍中定义的GradeBook类来实现: #include <iostream> using std::cout; using std::endl; #include <str ...

  5. UIStackView入门

    http://www.cocoachina.com/ios/20150623/12233.html

  6. SQL百分比

    百分比=cast(cast(count(ProvinceName)*100./ (select count(Id) from  dbo.TopicCurrent   where boardId=316 ...

  7. PostgreSQL auto_explain

    The auto_explain module provides a means for logging execution plans of slow statements automaticall ...

  8. ACM之Java速成(1)

    这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner cin = new Sc ...

  9. BZOJ K大数查询(分治)(Zjoi2013)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3110 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b ...

  10. ls -l 列表信息详解

    我们平时用ls -l 命令查看一个目录下的文件和子目录的详悉信息时,会得到一个详细的文件和目录名列表.这个列表包含了文件的属性,所属用户,所属组,创建时间,文件大小等等信息.这些信息到底是什么意思呢? ...