我参与了一个在工作中使用PL/SQL和Oracle DB的内容管理系统的开发,我希望使用PHP和MySQL为我自己的内容管理系统复制相同的结构。我以前是一名PHP程序员,现在我正在努力掌握OOPHP。
我有三张桌子。一个代表一个页面。另一个表示可在页面中使用的模板,另一个表示内容。我已经输出了页面的页眉和页脚(它们存储在页面表中),但是现在我需要用内容填充页面。为此,我需要查询模板表并将html代码赋给一个变量。然后,我需要使用content表将分配给变量(来自templates表)的html代码中的特定标记替换为来自另一个表的内容。所以,举个例子:
我查询模板表并取出一个包含html结构的列。在这个结构中,有一些自生成的标记,比如i1、i3,它们与content表中的列相关。我的目标是将这些标记替换为content表中每个相关列的相关内容(因此i1将替换为相关行中i1列的内容。在每个页面的内容表中可以有多个条目,因此这些条目是按顺序排序的。
我目前正忙于从content表中提取与特定页面相关的所有内容的数组。我知道如何使用旧的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方法来做这件事,然后我如何去为每个模板进行标记的替换呢?
我的表结构可以在下面找到。希望这能让我更清楚地知道我在寻找什么:

我猜在某种程度上,我的查询可以调整为执行内部连接,以便也检索到templates表中的code列,但我以前并没有真正将连接与MySQLi一起使用,所以我不确定是否有一种特定的方法来编写语法。
发布于 2013-02-04 21:09:00
在Jim的帮助下,我想出了一个明确的解决方案:
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;
}
}由于单个页面上可能有多个内容项,每个内容项可能具有不同的模板,因此我将grabTheTemplate函数调用移到了while循环中,以便可以使用template_id列中的值来检索相关的模板。我还没有在我的测试服务器上尝试过这一点,但理论上它应该都可以工作。今天晚上我会试一试,如果有任何bug,我会编辑这篇文章。
发布于 2013-02-01 09:06:30
您仍然可以使用fetch进行循环。填充模板应该只是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;
}https://stackoverflow.com/questions/14637214
复制相似问题