Home  >  Article  >  Backend Development  >  How to generate website sitemap with CodeIgniter_PHP tutorial

How to generate website sitemap with CodeIgniter_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:14746browse

1. Created a controller named sitemap

Copy the code The code is as follows:

if (!defined('BASEPATH'))
exit ('No direct script access allowed');

class Sitemap extends CI_Controller{
public function __construct () {
parent::__construct();
$this->load->model('sitemapxml');
}

function index(){
$data['posts']=$this->sitemapxml->getArticle();
$data['categorys']=$this->sitemapxml->getCategory();
$this ->load->view('sitemap.php',$data);
}
}

First load the sitemapxml model class, and the index method calls two methods, respectively Get a list of articles and a list of categories to output in the template.

2. Create a model named sitemapxml

Copy the code The code is as follows:

class Sitemapxml extends CI_Model{
public function __construct() {
parent :: __construct();
$this->load->database();
}

public function getArticle(){
$this->db->select('ID,post_date,post_name');
$this->db->order_by( 'post_date', 'desc');
$result=$this->db->get('posts');
return $result->result_array();
}

public function getCategory(){
$this->db->select('c_sname');
$result=$this->db->get('category') ;
return $result->result_array();
}
}

Two methods are defined in the model to obtain the article list and category list.

3. Create a template named sitemap.php

Copy the code The code is as follows:





sitemap


echo htmlspecialchars('').'
';
echo htmlspecialchars('').'
';

//首页单独写一个url
echo htmlspecialchars('').'
';
echo htmlspecialchars(' ').'http://aa.sinaapp.com'.htmlspecialchars('').'
';
echo htmlspecialchars('').date('Y-m-d',time()).htmlspecialchars('').'
';
echo htmlspecialchars('').'daily'.htmlspecialchars('').'
';
echo htmlspecialchars('').'1'.htmlspecialchars('').'
';
echo htmlspecialchars('
').'
';

//类别页
foreach ($categorys as $category){
 echo htmlspecialchars('').'
';
 echo htmlspecialchars(' ').'http://aa.sinaapp.com/index.php/home/cat/'.$category['c_sname'].htmlspecialchars('').'
';
 echo htmlspecialchars('').date('Y-m-d',time()).htmlspecialchars('').'
';
 echo htmlspecialchars('').'weekly'.htmlspecialchars('').'
';
 echo htmlspecialchars('').'0.8'.htmlspecialchars('').'
';
 echo htmlspecialchars('
').'
';
}

//文章页
foreach ($posts as $post){
 echo htmlspecialchars('').'
';
 echo htmlspecialchars(' ').'http://aa.sinaapp.com/index.php/home/details/'.$post['post_name'].htmlspecialchars('').'
';
 echo htmlspecialchars('').date('Y-m-d',strtotime($post['post_date'])).htmlspecialchars('').'
';
 echo htmlspecialchars('').'weekly'.htmlspecialchars('').'
';
 echo htmlspecialchars('').'0.6'.htmlspecialchars('').'
';
 echo htmlspecialchars('
').'
';
}

//留言板
echo htmlspecialchars('').'
';
echo htmlspecialchars(' ').'http://aa.sinaapp.com/index.php/guest'.htmlspecialchars('').'
';
echo htmlspecialchars('').date('Y-m-d',time()).htmlspecialchars('').'
';
echo htmlspecialchars('').'weekly'.htmlspecialchars('').'
';
echo htmlspecialchars('').'0.5'.htmlspecialchars('').'
';
echo htmlspecialchars('
').'
';

echo htmlspecialchars('
');
?>



The most important thing is this template. According to the standard format of sitemap.

Then use a very stupid method to copy the generated html text (actually the display content of the xml file) to a new sitemap.xml file, format it, save it, and a standard sitemap.xml file. Because the SAE deployment application to be used does not support write operations in the directory, it can only be uploaded in this way. It will be ok after a while.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825171.htmlTechArticle1. Create a controller named sitemap. Copy the code as follows: ?php if (!defined(' BASEPATH')) exit ('No direct script access allowed'); class Sitemap extends CI_Controller{...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn