Page templates in PHP and MySQL
I have a blog written in PHP which uses MySQL as its' back-end. There is a template for blog posts stored in the database and the script pulls this content when it is needed. How can I make it so I do not need to store the template in my database and in my PHP file instead?
My PHP code is:
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=@mysql_select_d开发者_如何转开发b($dbname, @mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = mysql_fetch_row(mysql_query("SELECT template FROM templates WHERE name = 'blog'"));
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}
}
?>
Here is the code stored in the database:
<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>
My question is; How can I make it so I do not need to store the template in my database and in my PHP file instead?
You mean like this?
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=@mysql_select_db($dbname, @mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = "<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>";
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp)) . " <?php ");
}
}
?>
精彩评论