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的模板
echo htmlspecialchars('').'
';
echo htmlspecialchars('
';
//首页单独写一个url
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
//类别页
foreach ($categorys as $category){
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
}
//文章页
foreach ($posts as $post){
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
}
//留言板
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
';
echo htmlspecialchars('
?>
最重要的就是这个模板了,按照sitemap.xml的标准格式,从数据库中读取相关数据,用循环的方式自动生成这样的格式,页面上展示的是html形式的xml的内容。
然后再用一个很笨的方法,将生成的html文本(实际上就是xml文件的显示内容),复制到一个新建的sitemap.xml文件,格式化一下,保存,就产生了一个标准的sitemap.xml文件。因为要用的SAE部署应用,目录不支持写操作,只能这样上传了,隔一段时间这样弄一下就ok了。

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

到Improveyourphpwebsite的實力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)優化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

是的,ItispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailsending.2)enasledeLaysBetenemailstoavoidSpamflagssspamflags.3))

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

使用PHP發送電子郵件的最佳方法包括:1.使用PHP的mail()函數進行基本發送;2.使用PHPMailer庫發送更複雜的HTML郵件;3.使用SendGrid等事務性郵件服務提高可靠性和分析能力。通過這些方法,可以確保郵件不僅到達收件箱,還能吸引收件人。

計算PHP多維數組的元素總數可以使用遞歸或迭代方法。 1.遞歸方法通過遍歷數組並遞歸處理嵌套數組來計數。 2.迭代方法使用棧來模擬遞歸,避免深度問題。 3.array_walk_recursive函數也能實現,但需手動計數。

在PHP中,do-while循環的特點是保證循環體至少執行一次,然後再根據條件決定是否繼續循環。 1)它在條件檢查之前執行循環體,適合需要確保操作至少執行一次的場景,如用戶輸入驗證和菜單系統。 2)然而,do-while循環的語法可能導致新手困惑,且可能增加不必要的性能開銷。

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函數進行快速哈希,但不適合密碼存儲。 2.使用sha256函數提高安全性。 3.使用password_hash函數處理密碼,提供最高安全性和便捷性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)