CodeIgniter method for generating site sitemap maps

  • 2020-11-26 18:44:05
  • OfStack

1. Set up a controller named sitemap


<?php
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);
 }
}

The sitemapxml model class is loaded first, and the index method calls two methods to get the list of articles and the list of categories, respectively, for output in the template.

Create a model called sitemapxml


<?php
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 get a list of articles and a list of categories.

3. Create a template named sitemap.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sitemap</title>
</head>
<body>
<?php
echo htmlspecialchars('<?xml version="1.0" encoding="utf-8"?>').'<br/>';
echo htmlspecialchars('<urlset>').'<br/>';

// Home page alone 1 a url
echo htmlspecialchars('<url>').'<br/>';
echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com'.htmlspecialchars('</loc>').'<br/>';
echo htmlspecialchars('<lastmod>').date('Y-m-d',time()).htmlspecialchars('</lastmod>').'<br/>';
echo htmlspecialchars('<changefreq>').'daily'.htmlspecialchars('</changefreq>').'<br/>';
echo htmlspecialchars('<priority>').'1'.htmlspecialchars('</priority>').'<br/>';
echo htmlspecialchars('</url>').'<br/>';

// Category page 
foreach ($categorys as $category){
 echo htmlspecialchars('<url>').'<br/>';
 echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com/index.php/home/cat/'.$category['c_sname'].htmlspecialchars('</loc>').'<br/>';
 echo htmlspecialchars('<lastmod>').date('Y-m-d',time()).htmlspecialchars('</lastmod>').'<br/>';
 echo htmlspecialchars('<changefreq>').'weekly'.htmlspecialchars('</changefreq>').'<br/>';
 echo htmlspecialchars('<priority>').'0.8'.htmlspecialchars('</priority>').'<br/>';
 echo htmlspecialchars('</url>').'<br/>';
}

// The article page 
foreach ($posts as $post){
 echo htmlspecialchars('<url>').'<br/>';
 echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com/index.php/home/details/'.$post['post_name'].htmlspecialchars('</loc>').'<br/>';
 echo htmlspecialchars('<lastmod>').date('Y-m-d',strtotime($post['post_date'])).htmlspecialchars('</lastmod>').'<br/>';
 echo htmlspecialchars('<changefreq>').'weekly'.htmlspecialchars('</changefreq>').'<br/>';
 echo htmlspecialchars('<priority>').'0.6'.htmlspecialchars('</priority>').'<br/>';
 echo htmlspecialchars('</url>').'<br/>';
}

// Message board 
echo htmlspecialchars('<url>').'<br/>';
echo htmlspecialchars(' <loc>').'http://aa.sinaapp.com/index.php/guest'.htmlspecialchars('</loc>').'<br/>';
echo htmlspecialchars('<lastmod>').date('Y-m-d',time()).htmlspecialchars('</lastmod>').'<br/>';
echo htmlspecialchars('<changefreq>').'weekly'.htmlspecialchars('</changefreq>').'<br/>';
echo htmlspecialchars('<priority>').'0.5'.htmlspecialchars('</priority>').'<br/>';
echo htmlspecialchars('</url>').'<br/>';

echo htmlspecialchars('</urlset>');
?>
</body>
</html>

The most important is this template, which reads the relevant data from the database in the standard format of ES20en.xml and automatically generates this format in a circular manner, displaying the xml content in the form of html on the page.

Then, with a very stupid method, copy the generated html text (actually the display content of xml file) to a newly created ES28en.xml file, format 1, save, and produce a standard ES30en.xml file. Because of the SAE application to be deployed, the directory does not support write operations, so it has to be uploaded, and every once in a while it will be ok.


Related articles: