PHP gets JSON file data and dynamically modifies website header file meta information--based on CI framework

Source: Internet
Author: User

Words don't say much. Just go ahead (if you have Chinese.) Please note that JSON only UTF-8 encoded)

First you need to have a JSON file data

{
"Index": {
"title": "Indexmytitle",
"keywords": "Chinese",
"Content": "Chinese",
"description": "Chinese"
},


        "goods": {
            & nbsp   "title": "Goodsmytitle",
                "keywords": "Goodskeywords", br>                  "content": "Goodsmycontent",
      &NBS P         "description": "Goodsmydes"
           },

"Shop": {
"title": "Shopmytitle",
"keywords": "Shopkeywords",
"Content": "Shopmycontent",
"description": "Shopmudes",
"Description1": "Shopmudes"
}


}

And then what. You need to build a public class in CI, Appliction/librarys directory 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/project name/public/json.json '; File path
$json _result= file_get_contents ($path); Read the JSON file into a string.
$json _array= Json_decode ($json _result,true); Json_decode encode strings in JSON format, convert to array form
return $json _array[$seo _category]; Returns the result based on the key value
}

}

?>

If you can't see it on top. That's the equivalent of the two, but it's more troublesome. If you change it, you change the class library.

<?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 '; File path
$json _result= file_get_contents ($path); Read the JSON file into a string.
$json _array= Json_decode ($json _result,true); Json_decode encode strings in JSON format, convert to array form

Two-dimensional array to one-dimensional array
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;
}

}
}

?>

And then look inside the controller,

Public Function __construct () {

$this->load->library (' json '); Loading the JSON Data class library

}

Called in the bottom method, and passed the value to the HTML view inside

$top [' Json_result ']= $this->json->some_function (' index ');

$this->load->view ("Templates/top", $top);

Last viewed View

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

Original works. For learning purposes only,

PHP gets JSON file data and dynamically modifies website header file meta information--based on CI framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.