I decided that objects would be the best thing to use for the forum I'm making (easier/safer to make templates), but came into a problem - the forums themselves! But, as I have very little experience with objects, I was bound to come into some obstacles 🙁
I need a way to loop through the topics, and each time, including and replacing the specific parts of the HTML page with the PHP generated parts. That probably didn't make much sense, so I'll just show you what I have atm, and what kind of works...
Main PHP:
$forum = new Template("templates/$cur_template/viewforum.tpl");
$content = '';
// Table headers here (title, replies etc etc)
$result2 = // Query to to select topics from DB
$num = mysql_num_rows($result2);
if ($num > 0) {
$i = 0;
while ($row2 = mysql_fetch_array($result2)) {
$i++;
// Set the parameters
$forum->set_params(array(
"TOPIC_ICON" => $row2['icon'],
"TOPIC_ID" => $row2['topic_id'],
"TOPIC_NAME" => $row2['name'],
"TOPIC_DESC" => $row2['description'],
"TOPIC_STARTER" => $row2['starter'],
"TOPIC_REPLIES" => $row2['replies'],
"TOPIC_VIEWS" => $row2['views'],
"TOPIC_LPOST" => $row2['last_post'],
"TOPIC_I" => $i,
));
$content .= $forum->swap_params();
}
echo $content;
} else {
// No topics etc...
}
($i variable was just there for debugging, seeing how it was looping etc)
set_params Method:
function set_params($vararray)
{ // Sets the particular values
reset ($vararray);
foreach($vararray as $key => $value) {
$this->parameters[$key] = $value;
}
}
swap_params Method:
function swap_params()
{
foreach($this->parameters as $key => $value) {
$template_name = '{' . $key . '}';
$this->html = str_replace($template_name, $value, $this->html);
}
return $this->html;
}
If you need any more information, just ask.
Thanks 🙂
EDIT: Forgot to say, this currently prints out the correct amount of rows (topics), but uses the data from the first for all of them (so I'm presuming the values of the variables aren't being overwritten...or something 😐)