[英]Looping through the results of a mysqli query and replacing content tags with table columns
我參與了使用PL / SQL和Oracle DB開發CMS的工作,並且希望使用PHP和MySQL為我自己的CMS復制相同類型的結構。 我是舊程序的PHP程序員,而現在我正試圖與OOPHP保持聯系。
我有三張桌子。 一個代表一頁。 另一個代表可以在頁面中使用的模板,另一個代表內容。 我已經盡力輸出頁面的頁眉和頁腳(存儲在頁面表中),但是現在我需要用內容填充頁面。 為此,我需要查詢模板表並將html代碼分配給變量。 然后,我需要使用內容表將分配給變量(來自模板表)的html代碼中的特定標記替換為另一個表的內容。 因此,例如:
我查詢模板表,並拉出包含html結構的列。 在此結構中,存在與內容表中的列相關的自生成標簽,例如[i1] [i2] [i3]。 我的目標是將這些標簽替換為內容表中每個相關列的相關內容(因此[i1]將被相關行中i1列中的內容替換。該內容中可以有多個條目每頁的表格,因此按順序排列。
我目前停留在從內容表中提取與特定頁面相關的所有內容的數組。 我知道如何使用舊的mysql界面以程序方式進行此操作,但我找不到有關如何在mysqli中正確執行操作的最新文章。 有小費嗎?
這是我當前的一組課程:
class Connection extends Mysqli{
public function __construct($mysqli_host,$mysqli_user,$mysqli_pass, $mysqli_db) {
parent::__construct($mysqli_host,$mysqli_user,$mysqli_pass,$mysqli_db);
$this->throwConnectionExceptionOnConnectionError();
$this->getUser();
}
private function throwConnectionExceptionOnConnectionError(){
if(!$this->connect_error){
echo "Database connection established<br/>";
}else{
//$message = sprintf('(%s) %s', $this->connect_errno, $this->connect_error);
echo "Error connecting to the database.";
throw new DatabaseException($message);
}
}
}
class DatabaseException extends Exception{
}
class Page {
public function __construct() {
if(isset($_GET['id'])){
$id = $_GET['id'];
}else{
$id = 1;
}
get_headers($id);
get_content($id);
get_footer($id);
}
private function get_headers($pageId){
$retrieveHead = $this->prepare("SELECT header FROM pages WHERE page_id=?");
$retrieveHead->bind_param('i',$pageId);
$retrieveHead->execute();
$retrieveHead->bind_result($header);
$retrieveHead->fetch();
$retrieveHead->close();
echo $header;
}
private function get_footer($pageId){
$retrieveFooter = $this->prepare("SELECT footer FROM pages WHERE page_id=?");
$retrieveFooter->bind_param('i',$pageId);
$retrieveFooter->execute();
$retrieveFooter->bind_result($footer);
$retrieveFooter->fetch();
$retrieveFooter->close();
echo $footer;
}
private function get_content($pageId){
$retreiveContent = $this->prepare("SELECT * FROM content WHERE page_id=? ORDER BY sequence");
$retreiveContent->bind_param('i',$pageId);
$retreiveContent->execute();
}
}
從我對mySQL的記憶中,從這里開始,我將進行for循環,但是我將如何使用這種OOP方法進行此操作,然后又如何對每個模板進行標記替換呢?
我的表結構可以在下面找到。 希望這將使我正在尋找的東西更加清楚:
我猜想我的查詢可以通過某種方式進行內部聯接,以使模板表中的代碼列也被檢索,但是我之前並沒有真正使用MySQLi的聯接,所以我不確定是否有一種特定的語法編寫方式。
您仍然可以使用訪存循環。 填充模板僅是str_replacing它們的一種情況:
private function get_content($pageId){
$theTemplate = grabTheTemplate();
$retreiveContent = $this->prepare("SELECT * FROM content WHERE page_id=? ORDER BY sequence");
$retreiveContent->bind_param('i',$pageId);
$retreiveContent->execute();
$retreiveContent->bind_result($foo, $bar,$baz);
while ($retreiveContent->fetch()) {
//$foo, $bar $baz will be populated for this row.
//Update the tags in the template.
$theTemplate = str_replace('tagToReplace','valueToReplaceWith',$theTemplate);
}
//$theTemplate is populated with content. Probably want to echo here
echo $theTemplate;
}
在吉姆的幫助下,我提出了最終的解決方案:
private function get_content($pageId){
$retreiveContent = $this->prepare("SELECT template_id, section_title, i1, i2 FROM content WHERE page_id=? ORDER BY sequence");
$retreiveContent->bind_param('i',$pageId);
$retreiveContent->execute();
$retreiveContent->bind_result($template_id, $section_title, $i1, $i2);
while ($retreiveContent->fetch()) {
//Variables will be populated for this row.
//Update the tags in the template.
$theTemplate = grabTheTemplate($template_id);
$theTemplate = str_replace('[i1]',$i1,$theTemplate);
$theTemplate = str_replace('[i2]',$i2,$theTemplate);
//$theTemplate is populated with content. Probably want to echo here
echo $theTemplate;
}
}
由於在單個頁面上可以有多個內容項,每個內容項可以具有不同的模板,因此我已將whileThe循環內的catchTheTemplate函數調用移至while循環內,以便可以使用template_id列中的值來檢索相關模板。 我尚未在測試服務器上嘗試過此操作,但從理論上講,它應該都能正常工作。 今天晚上我會嘗試一下,如果有任何錯誤,請編輯這篇文章。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.