CodeIgniter生成网站sitemap地图的方法_php技巧

WBOY
Freigeben: 2016-05-17 08:52:50
Original
1182 Leute haben es durchsucht

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了。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!