Shoutbox

why doesn't this work!!! - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: why doesn't this work!!! (/showthread.php?tid=47492)

why doesn't this work!!! by saralk on 07-12-2005 at 10:54 AM

* saralk waits for someone to point out a really obvious mistake

code:
$terms = array();
$terms = explode(" ", $search);

foreach ($terms as $value) {

    $text = str_replace($value, "<span style='background-color:#FFFF00'>$value</span>", $row["Text"]);

}

echo $text;


When I run that code, it only highlights the last word in the array, and not the ones before it.

Any Ideas?
RE: why doesn't this work!!! by Eljay on 07-12-2005 at 10:57 AM

use $text .= str_replace crap here;


RE: why doesn't this work!!! by saralk on 07-12-2005 at 11:00 AM

nope, that doesn't work it just adds $text again and the next time round it highlights the next word in the array.


RE: why doesn't this work!!! by Eljay on 07-12-2005 at 11:02 AM

whats $row[Text]?

show me the whole code


RE: why doesn't this work!!! by saralk on 07-12-2005 at 11:04 AM

code:
<?php

include "sql.php";
include "header.php";
$sql = "SELECT * FROM posts WHERE MATCH (Subject,Text) AGAINST('".$search."')";
$rs=mysql_query($sql,$conn);
mysql_error($conn);
echo "<div id='blogcontainer'>";
echo "<div id='blogtitle'>Search Results</div>";
echo "<div id='blogcontent'>You searched for <b>".$search."</b>, these words have been highlighted in the results below</div>";


while ($row = mysql_fetch_array($rs)) {
$terms = array();
$terms = explode(" ", $search);
foreach ($terms as $value) {
$text = $row["Text"];
$text = str_replace($value, "<span style='background-color: #FFFF00'>$value</span>", $text);
}
echo "<div id='blogtitle'>".$row["Subject"]."</div>";
echo "<span class='blogcontent'>";
echo $text;
echo "</span>";
echo "<div id='blogfoot'>Posted by ".$row["User"]." on ".$row["time"]." | <a href='view.php?newsid=".$row["newsid"]."'>Comment</a></div>";

}

echo "</div>";
include "footer.php";
?>

RE: why doesn't this work!!! by Chris.1 on 07-12-2005 at 01:29 PM

Everytime you run the foreach loop, you are overwriting the value of $text so that when it is echoed out only the last value run is shown.

Try changing it an array, then use print_r to print the array?


RE: why doesn't this work!!! by saralk on 07-12-2005 at 01:36 PM

I fixed it now, and it was something really simple :@

I just moved $text = $row["Text"]; outside the foreach section :p