search
HomeBackend DevelopmentPHP TutorialCodeIgniter生成网站sitemap地图的方法_php技巧

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.