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

<?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‘];?>">

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

时间: 2024-08-27 16:34:43

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

jQuery获取JSON格式数据方法

getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ $.getJSON("demo_ajax_json.js",function(result_data){ $.each(result_data, function(index, name_value){ $("div").append(name_value + &qu

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

MainWindow.xaml文件 1 <Window 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:

使用AngularJS结合Highchart图表动态获取JSON格式数据

Highchart折线图代码 $(function () { var chartLine = new Highcharts.Chart({ chart: { type: 'line', <em><span style="color:#ff0000;">renderTo: 'container2'</span></em> }, title: { text: '上月已还款金额与本月账单金额对比结果' }, subtitle: { text:

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

前几天做一个门户网站,在首页需要加载气象数据,采用了中央气象局的接口. 刚开始采用JSONP在前台跨域请求数据,没成功~ 后换成在c#后台请求数据返回... 前端代码: $(function () { $.ajax({ type: "GET", url: "service/getWeather.ashx", dataType: "json", success: function (data) { var weatherMS = ''; conso

jQuery调用ajax获取json格式数据

<body> <div>点击按钮获取音乐列表</div> <input type="button" id="button" value="确定" /> <div id="result"></div> <div>添加新的音乐</div> <input type="text" name=""

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

<html xmlns="http://www.w3.org/1999/xhtml">   <head runat="server">   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   <title></title>   </head>   <body&

SCCM客户端ccmcache文件夹清除和修改及Installer文件夹大小问题

关于ccmcache文件夹大小问题: 一大早上班收到邮件告警,提示OA系统C盘空间不足,使用率超过85% 查看C盘文件使用情况,发现ccmcache占用了7.4G的空间 ccmcache是sccm客户端更新时下载补丁用的缓存文件夹,将临时补丁.驱动和其他下载的软件存储在计算机或移动设备上. 清除ccmcache会删除过时的软件和信息,释放系统空间,并帮助消除任何有害的恶意软件.但注意:清除缓存可能会影响您之前在CCM附属程序中保存的个人信息和密码设置. 网上查了一下,ccmcache文件夹内容可

Vue动态添加v-model绑定及获取其返回数据

从数据库拿到的动态数据绑定到页面对应的v-model或者v-bind上,并且根据对页的操作获取到返回的值: 1.首先在data里定义一个数据 timeTip 为一个空数组 data () { return { timeTip:[] } } 2.将获取到的数据进行动态生成,并塞入 timeTip 中 creadeTimeTip(data.data) //返回的数据 creadeTimeTip(data){ this.timeTip = []; //对空数组进行清空,以免影响后续操作 var len

修改TrustedInstaller权限文件(无法删除文件)

1.    右击需要修改的文件-属性 2.    切换到"安全"选项卡,点击"高级"按钮. 3.    切换到"所有者"选项卡 一般情况下默认所有者为TrustedInstaller(没有影响),单击"编辑"按钮 4.    弹出的窗口中选择当前计算机的登陆用户名,点击确定 5.    如果弹出如图提示窗口,直接点确定 6.    后面的窗口都点击确定 7.    再次右击该文件,属性,安全,编辑 8.    选择当前用户名,