CodeIgniter產生網站sitemap地圖的方法_PHP教程

來源:互聯網
上載者:User
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的模板
複製代碼 代碼如下:




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('');
?>



最重要的就是這個模板了,按照sitemap.xml的標準格式,從資料庫中讀取相關資料,用迴圈的方式自動產生這樣的格式,頁面上展示的是html形式的xml的內容。

然後再用一個很笨的方法,將產生的html文本(實際上就是xml檔案的顯示內容),複製到一個建立的sitemap.xml檔案,格式化一下,儲存,就產生了一個標準的sitemap.xml檔案。因為要用的SAE部署應用,目錄不支援寫操作,只能這樣上傳了,隔一段時間這樣弄一下就ok了。

http://www.bkjia.com/PHPjc/825171.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825171.htmlTechArticle1.建立了一個名為sitemap的控制器 複製代碼 代碼如下: ?php if (!defined('BASEPATH')) exit ('No direct script access allowed'); class Sitemap extends CI_Controller{...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.