I am involved in the development of a CMS at work using PL/SQL and an Oracle DB and I wish to replicate the same sort of structure for my own CMS using PHP and MySQL. I’m a PHP programmer from the old procedural days and i’m now trying to get to grips with OOPHP.
I have three tables. One represents a page. Another represents the templates that can be used within a page and the other represents the content. I’ve got as far as outputting the header and footer of the page (which are stored within the page table) however I now need to populate the page with content. To do this, I need to query the template table and assign the html code to a variable. I then need to use the content table to replace specific tags in the html code that is assigned to the variable (from the templates table) with the content from another table. So, for example:
I query the template table and pull out a column which contains a html structure. Within this structure there are self-generated tags such as [i1] [i2] [i3] which relate to columns in the content table. What I am aiming to do is replace these tags with the relevant contents of each relevant column in the content table (so [i1] would be replaced with the content from the i1 column in the relevant row. There can be multiple entries in the content table for each page so these are ordered by sequence.
I’m currently stuck at pulling an array of all content that is relevant to the specific page from the content table. I know how to do this procedurally using the old mysql interface but I can’t find any recent posts as to how to do it properly in mysqli. Any tips?
Here is my current set of classes:
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();
}
}
From what I can remember of mySQL, from here I would do a for loop but how would I go about doing this using this OOP approach and how would I then go about conducting the replacement of tags for each template?
My table structure can be found below. Hopefully this will make it more clear what I am looking for:

I’m guessing that in some way my query could be adapted to perform an inner join so that the code column from the templates table is also retreived but I’ve not really used joins with MySQLi before so I’m not sure if there is a specific way to write the syntax.
With Jim’s help I have come up with the definitive solution:
As there can be multiple content items on a single page each of which could have a different template, I have moved the grabTheTemplate function call inside of the while loop so that the value from the template_id column can be used to retrieve the relevant template. I haven’t tried this on my test server yet but in theory it should all work. I will try it this evening and edit this post if there are any bugs.