Jump to content

Replace several words in HTML using str_replace before displaying it


ScorchPipe

Recommended Posts

Hello

 

I'm making a guestbook in PHP and I'm having trouble displaying the posts. The user fills a form and sends it, it goes into the database (mysql) and I can then fetch the data. So far so good.

 

I'm then going to use HTML to display the posts to the user. It's code looking like this:

 

<p>
<br /> ID: ---replaceid--- <br /><br />

Time: ---replacetime--- <br />
From: <a href="---replacehomepage---">---replacefrom---</a> <br />
Email: ---replacemail--- <br /> <br />

Comment: ---replacecomment--- <br /><hr>
</p>

 

I want to use str_replace in PHP to replace the values in HTML... like this:

$numquery = mysql_query("SELECT * FROM guestbook", $dbconnect);

$num = mysql_num_rows($numquery);
$i=1;
while ($i < $num) {

header('Content-type: text/html');
$html = file_get_contents("test.html");

$query = mysql_query("SELECT * FROM guestbook WHERE id='$i'", $dbconnect);
$rows = mysql_fetch_row($query);

echo str_replace('---replaceid---', nl2br($rows[0]), $html);
echo str_replace('---replacetime---', nl2br($rows[1]), $html);
echo str_replace('---replacefrom---', nl2br($rows[2]), $html);
echo str_replace('---replacemail---', nl2br($rows[3]), $html);
echo str_replace('---replacehomepage---', nl2br($rows[4]), $html);
echo str_replace('---replacecomment---', nl2br($rows[5]), $html);

$i++;
}

 

But this only replaces ---replaceid---, leaves the rest and outputs it. Then, only ---replacetime--- is replaced and so on.

I hope you understand. It means that 1 guestbook entry is displayed like 6, with only 1 string replaced at each one.

 

What do you think I should do?

 

Worth saying is that I'm not allowed to mix PHP-code with HTML-code

$htmlx =  str_replace('---replaceid---', nl2br($rows[0]), $html);

$htmlx =  str_replace('---replacetime---', nl2br($rows[1]), $htmlx);

$htmlx =  str_replace('---replacefrom---', nl2br($rows[2]), $htmlx);

$htmlx =  str_replace('---replacemail---', nl2br($rows[3]), $htmlx);

$htmlx =  str_replace('---replacehomepage---', nl2br($rows[4]), $htmlx);

$htmlx =  str_replace('---replacecomment---', nl2br($rows[5]), $htmlx);

echo $htmlx;

 

but why..... do it this way it would cause the page to load to slow.....

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.