What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » why doesn't this work!!!

why doesn't this work!!!
Author: Message:
saralk
Veteran Member
*****

Avatar

Posts: 2598
Reputation: 38
35 / Male / Flag
Joined: Feb 2003
O.P. why doesn't this work!!!
* 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?

This post was edited on 07-12-2005 at 10:55 AM by saralk.
The Artist Formerly Known As saralk
London · New York · Paris
Est. 1989
07-12-2005 10:54 AM
Profile PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: why doesn't this work!!!
use $text .= str_replace crap here;
07-12-2005 10:57 AM
Profile PM Find Quote Report
saralk
Veteran Member
*****

Avatar

Posts: 2598
Reputation: 38
35 / Male / Flag
Joined: Feb 2003
O.P. RE: why doesn't this work!!!
nope, that doesn't work it just adds $text again and the next time round it highlights the next word in the array.
The Artist Formerly Known As saralk
London · New York · Paris
Est. 1989
07-12-2005 11:00 AM
Profile PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: why doesn't this work!!!
whats $row[Text]?

show me the whole code
07-12-2005 11:02 AM
Profile PM Find Quote Report
saralk
Veteran Member
*****

Avatar

Posts: 2598
Reputation: 38
35 / Male / Flag
Joined: Feb 2003
O.P. RE: why doesn't this work!!!
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";
?>
The Artist Formerly Known As saralk
London · New York · Paris
Est. 1989
07-12-2005 11:04 AM
Profile PM Find Quote Report
Chris.1
Full Member
***

Avatar
addicted to the net

Posts: 363
Reputation: 3
37 / Male / –
Joined: Jun 2003
RE: why doesn't this work!!!
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?
07-12-2005 01:29 PM
Profile E-Mail PM Web Find Quote Report
saralk
Veteran Member
*****

Avatar

Posts: 2598
Reputation: 38
35 / Male / Flag
Joined: Feb 2003
O.P. RE: why doesn't this work!!!
I fixed it now, and it was something really simple :@

I just moved $text = $row["Text"]; outside the foreach section :p
The Artist Formerly Known As saralk
London · New York · Paris
Est. 1989
07-12-2005 01:36 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On