这篇文章主要介绍CodeIgniter如何生成网站sitemap地图,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1.建立了一个名为sitemap的控制器
代码如下:
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);
 }
}
首先加载sitemapxml模型类,index方法调用两个方法,分别获取文章列表和类别列表,以在模板中输出。
2.创建一个名为sitemapxml的模型
代码如下:
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();
 }
}
模型里面定义两个方法,获取文章列表和类别列表。
3.创建一个名为sitemap.php的模板
代码如下:
echo htmlspecialchars('').'
';
echo htmlspecialchars('
';
//首页单独写一个url
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
//类别页
foreach ($categorys as $category){
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
}
//文章页
foreach ($posts as $post){
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
}
//留言板
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
?>

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询