国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

CodeIgniter生成網(wǎng)站sitemap地圖的方法

2024-05-04 23:19:37
字體:
供稿:網(wǎng)友

1.建立了一個名為sitemap的控制器

復(fù)制代碼 代碼如下:


<?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)用兩個方法,分別獲取文章列表和類別列表,以在模板中輸出。

2.創(chuàng)建一個名為sitemapxml的模型

復(fù)制代碼 代碼如下:


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


模型里面定義兩個方法,獲取文章列表和類別列表。

3.創(chuàng)建一個名為sitemap.php的模板

復(fù)制代碼 代碼如下:


<!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/>';

//首頁單獨寫一個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/>';

//類別頁
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/>';
}

//文章頁
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>


最重要的就是這個模板了,按照sitemap.xml的標準格式,從數(shù)據(jù)庫中讀取相關(guān)數(shù)據(jù),用循環(huán)的方式自動生成這樣的格式,頁面上展示的是html形式的xml的內(nèi)容。

然后再用一個很笨的方法,將生成的html文本(實際上就是xml文件的顯示內(nèi)容),復(fù)制到一個新建的sitemap.xml文件,格式化一下,保存,就產(chǎn)生了一個標準的sitemap.xml文件。因為要用的SAE部署應(yīng)用,目錄不支持寫操作,只能這樣上傳了,隔一段時間這樣弄一下就ok了。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 眉山市| 哈尔滨市| 巫山县| 溧水县| 丹巴县| 铅山县| 贵州省| 昂仁县| 广州市| 上栗县| 久治县| 广灵县| 闽侯县| 苏尼特右旗| 象山县| 雷州市| 客服| 南雄市| 双牌县| 大渡口区| 泌阳县| 长乐市| 琼结县| 湖南省| 鄢陵县| 中方县| 宿松县| 金塔县| 察隅县| 桐乡市| 辉县市| 温宿县| 郑州市| 潜山县| 长顺县| 上蔡县| 济宁市| 常熟市| 巴林左旗| 铜梁县| 大悟县|