How CodeIgniter generates site map

Source: Internet
Author: User
Tags codeigniter
It takes only three steps to use CodeIgniter to generate a website sitemap. the method is very simple. you can refer to the CI framework.

1. a controller named sitemap is created.
The code is as follows:
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 ['category'] = $ this-> sitemapxml-> getCategory ();
$ This-> load-> view ('sitemap. php', $ data );
}
}

First, load the sitemapxml model class. The index method calls two methods to obtain the article list and category list respectively, and output them in the template.

2. create a model named sitemapxml
The code is as follows:
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 ();
}
}

The model defines two methods to obtain the document list and category list.

3. create a template named sitemap. php
The code is as follows:




Sitemap


Echo htmlspecialchars (' ').'
';
Echo htmlspecialchars (' ').'
';

// Write a url on the homepage
Echo htmlspecialchars (' ').'
';
Echo htmlspecialchars (' '). 'Http: // aa.sinaapp.com'.html specialchars (' ').'
';
Echo htmlspecialchars (' '{.Date('y-m-d', time({}.html specialchars (' ').'
';
Echo htmlspecialchars (' '0000.'daily'.html specialchars (' ').'
';
Echo htmlspecialchars (' '{.'1'.html specialchars (' ').'
';
Echo htmlspecialchars (' ').'
';

// Category page
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({}.html specialchars (' ').'
';
Echo htmlspecialchars (' 'Privacy .'weekly'.html specialchars (' ').'
';
Echo htmlspecialchars (' '0000.'0.8'.html specialchars (' ').'
';
Echo htmlspecialchars (' ').'
';
}

// Article page
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'{}}.html specialchars (' ').'
';
Echo htmlspecialchars (' 'Privacy .'weekly'.html specialchars (' ').'
';
Echo htmlspecialchars (' '0000.'0.6'.html specialchars (' ').'
';
Echo htmlspecialchars (' ').'
';
}

// Message board
Echo htmlspecialchars (' ').'
';
Echo htmlspecialchars (' '). 'Http: // aa.sinaapp.com/index.php/guest'.htmlspecialchars (' ').'
';
Echo htmlspecialchars (' '{.Date('y-m-d', time({}.html specialchars (' ').'
';
Echo htmlspecialchars (' 'Privacy .'weekly'.html specialchars (' ').'
';
Echo htmlspecialchars (' '0000.'0.5'.html specialchars (' ').'
';
Echo htmlspecialchars (' ').'
';

Echo htmlspecialchars (' ');
?>



The most important thing is this template, according to sitemap. the standard xml format reads relevant data from the database and automatically generates such a format in a loop. the page displays xml content in html format.

Then, copy the generated html text (actually the content displayed in the xml file) to a new sitemap. xml file, formatted, and saved, a standard sitemap is generated. xml file. Because the SAE to be used to deploy the application, the directory does not support write operations, so the upload is only allowed, and it will be OK after a while.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.