CodeIgniter生成網(wǎng)站sitemap地圖的方法
1.建立了一個(gè)名為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);
}
}
首先加載sitemapxml模型類,index方法調(diào)用兩個(gè)方法,分別獲取文章列表和類別列表,以在模板中輸出。
2.創(chuàng)建一個(gè)名為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();
}
}
模型里面定義兩個(gè)方法,獲取文章列表和類別列表。
3.創(chuàng)建一個(gè)名為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/>';
//首頁(yè)單獨(dú)寫一個(gè)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/>';
//類別頁(yè)
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/>';
}
//文章頁(yè)
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/>';
}
//留言板
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>
最重要的就是這個(gè)模板了,按照sitemap.xml的標(biāo)準(zhǔn)格式,從數(shù)據(jù)庫(kù)中讀取相關(guān)數(shù)據(jù),用循環(huán)的方式自動(dòng)生成這樣的格式,頁(yè)面上展示的是html形式的xml的內(nèi)容。
然后再用一個(gè)很笨的方法,將生成的html文本(實(shí)際上就是xml文件的顯示內(nèi)容),復(fù)制到一個(gè)新建的sitemap.xml文件,格式化一下,保存,就產(chǎn)生了一個(gè)標(biāo)準(zhǔn)的sitemap.xml文件。因?yàn)橐玫腟AE部署應(yīng)用,目錄不支持寫操作,只能這樣上傳了,隔一段時(shí)間這樣弄一下就ok了。
- Codeigniter生成Excel文檔的簡(jiǎn)單方法
- PHP生成HTML靜態(tài)頁(yè)面實(shí)例代碼
- html靜態(tài)頁(yè)面調(diào)用php文件的方法
- 比較詳細(xì)PHP生成靜態(tài)頁(yè)面教程
- PHP生成靜態(tài)頁(yè)
- CodeIgniter連貫操作的底層原理分析
- CodeIgniter記錄錯(cuò)誤日志的方法全面總結(jié)
- php基于CodeIgniter實(shí)現(xiàn)圖片上傳、剪切功能
- CI(CodeIgniter)框架中的增刪改查操作
- 在CODEIGNITER中 在CI中引入外部的JS與CSS呢
- CodeIgniter生成靜態(tài)頁(yè)的方法
相關(guān)文章
一道求$b相對(duì)于$a的相對(duì)路徑的php代碼
這是一段計(jì)算兩個(gè)路徑的相對(duì)路徑的php代碼,需要的朋友可以參考下。2010-08-08PHP操作文件類的函數(shù)代碼(文件和文件夾創(chuàng)建,復(fù)制,移動(dòng)和刪除)
PHP操作文件類(文件和文件夾創(chuàng)建,復(fù)制,移動(dòng)和刪除) ,使用也比較方便,需要的朋友可以參考下。2011-11-11PHP 字符串正則替換函數(shù)preg_replace使用說(shuō)明
PHP 字符串正則替換函數(shù)preg_replace使用說(shuō)明,需要的朋友可以參考下。2011-07-07PHP實(shí)現(xiàn)的常規(guī)正則驗(yàn)證helper公共類完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的常規(guī)正則驗(yàn)證helper公共類,結(jié)合完整實(shí)例形式分析了php針對(duì)常規(guī)的電話、手機(jī)、郵箱、賬號(hào)等進(jìn)行正則驗(yàn)證的操作技巧,需要的朋友可以參考下2017-04-04