ScorchPipe Posted February 25, 2012 Share Posted February 25, 2012 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 Link to comment https://forums.phpfreaks.com/topic/257766-replace-several-words-in-html-using-str_replace-before-displaying-it/ Share on other sites More sharing options...
blacknight Posted February 26, 2012 Share Posted February 26, 2012 $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..... Link to comment https://forums.phpfreaks.com/topic/257766-replace-several-words-in-html-using-str_replace-before-displaying-it/#findComment-1321296 Share on other sites More sharing options...
ScorchPipe Posted February 26, 2012 Author Share Posted February 26, 2012 Thanks for the help! I should've figured that one out =) I'm doing it like this because it was the best method I could come up with without putting HTML code in PHP or vice versa Link to comment https://forums.phpfreaks.com/topic/257766-replace-several-words-in-html-using-str_replace-before-displaying-it/#findComment-1321392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.