View Full Version : PHP (ereg_replace, site variables... huge mess)


hdshngout
09-24-2006, 04:51 AM
I am redoing Mindless Blogs to make it more customizable to users. One way to do this (or at least I believed) was to create special variables to be replaced with functions. Like %display_entries% would be replaced with the list of entries. But of course I ran into a snag.


define('secure','true');
include("config.php");

mysql_connect($db_host,$db_username,$db_password);
mysql_select_db($db_name);


$sql = "SELECT * FROM blog_coding WHERE username='$_GET[username]'";
$result = mysql_query($sql) or print(mysql_error());

while($row = mysql_fetch_array($result)) {
$header = html_entity_decode(stripslashes($row['header']));
$left = html_entity_decode(stripslashes($row['left']));
$right = html_entity_decode(stripslashes($row['right']));
$center = html_entity_decode(stripslashes($row['center']));
$entries = html_entity_decode(stripslashes($row['entries']));
$footer = html_entity_decode(stripslashes($row['footer']));
$theme = html_entity_decode(stripslashes($row['theme']));

$header = ereg_replace('%styles%', '<style type="text/css">' . $theme . '</style>', $header);

$header = ereg_replace('%username%', $_GET['username'], $header);
$header = ereg_replace('%rss_feed%', '<link href="' . $address.'/'. $_GET[username] . '.rss" type="application/rss+xml" title="RSS 2.0" />', $header);

$center = ereg_replace('%display_entries%', $entries, $center);

}

mysql_connect($db_host,$db_username,$db_password);
mysql_select_db($db_name);


$sql2 = "SELECT * FROM entries WHERE author='$_GET[username]' ORDER BY timestamp";
$result2 = mysql_query($sql2) or print(mysql_error());
while($row2 = mysql_fetch_array($result2)) {

$title = html_entity_decode(stripslashes($row2['title']));
$text = html_entity_decode(stripslashes($row2['text']));
$timestamp = html_entity_decode(stripslashes($row2['timestamp']));

$entries = ereg_replace('%entry_title%', $title, $entries);
$entries = ereg_replace('%entry_text%', $text, $entries);
$entries = ereg_replace('%entry_timestamp%', $timestamp, $entries);

echo $entries;
}

echo $header;
echo $left;
echo $center;
echo $right;



Lets see if I can explain this. The first sql query selects data from the blog_coding table (which makes up the blogs layout). Codes like %style% would be replaced with an inline stylesheet etc).

In the $center variable however, there is a site-only variable called %display_entries% which I was hoping could be parsed then replaced with a query which displays the entries.

Then to go further, the entires would be customizable via %entry_title%, %entry_text%, and %entry_timestamp% (all of which would be replaced with their respectable information).

This isn't working! What happens with this coding is I get a display similar to

Entry 1 Entry 1 1159070520

Entry 1 Entry 1 1159070520

Entry 1 Entry 1 1159070520


For some reason 3 of the same entry are appearing (and even more strange are that there are only 2 entries in the database). If anyone can think of a better way to do this, or a reason why its displaying stuff weird, please help!

ponygrl2
09-24-2006, 04:18 PM
Maybe i can help...
I noticed in the while loop there was no else statement to execute when there was no other data that could be why it displays the same entry 3 times
I could be wrong but i think that might be the problem

dschreck
09-26-2006, 04:30 PM
I would attempt to answer this, but you're not giving that much info... maybe we could see the db table layout?