jd2007 Posted July 12, 2007 Share Posted July 12, 2007 $fieldvalue changes every time it loops. i want to use php to see if the same $fieldvalue content is already echoed, then stop $fieldvalue from being echoed. how to do this, pls help ? $db = mysql_connect("localhost", "root"); $connection=mysql_select_db("Search", $db); $i=$namelen; for ($j=0; $j<$i; $j++) { $query = "SELECT Searching.page_title, Searching.info, Searching.link FROM Searching WHERE info LIKE'%".$name[$j]."%'"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { //echo "<br>"; } while ($record = mysql_fetch_assoc($result)) { while (list($fieldname, $fieldvalue) = each ($record)) { for ($l=0; $l<count($name); $l++) { $old = array($name[$l]); $new = array("<b><u>".$name[$l]."</u></b>"); $fieldvalue = str_replace($old, $new, $fieldvalue); } echo $fieldvalue."<br>"; } echo "<br>"; } $name is an array....the code above is executes if the user keys in more than one word in a search box...the words stored as a string is exploded into the $name array... Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 12, 2007 Share Posted July 12, 2007 Hi! After you used echo $fieldvalue you can add the value in an array... make it like that: <?php //your code $echoed = array(); while ($record = mysql_fetch_assoc($result)) { while (list($fieldname, $fieldvalue) = each ($record)) { for ($l=0; $l<count($name); $l++) { $old = array($name[$l]); $new = array("<b><u>".$name[$l]."</u></b>"); $fieldvalue = str_replace($old, $new, $fieldvalue); } if (!in_array($fieldvalue, $echoed) { //value already echoed? echo $fieldvalue."<br>"; $echoed[] = $fieldvalue; //add value into array } } echo "<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
jd2007 Posted July 12, 2007 Author Share Posted July 12, 2007 thanks....what do u think of my code ? is it good code or bad code ? Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 12, 2007 Share Posted July 12, 2007 bad... because you create the db-connection in the same file... It's clearer to define and put up a connection in another file and just include it by require_once or include_once. Quote Link to comment Share on other sites More sharing options...
jd2007 Posted July 12, 2007 Author Share Posted July 12, 2007 its not working... Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 its not working... Post your code then! Quote Link to comment Share on other sites More sharing options...
jd2007 Posted July 12, 2007 Author Share Posted July 12, 2007 $echoed=array(); while ($record = mysql_fetch_assoc($result)) { while (list($fieldname, $fieldvalue) = each ($record)) { for ($l=0; $l<count($name); $l++) { $old = array($name[$l]); $new = array("<b><u>".$name[$l]."</u></b>"); $fieldvalue = str_replace($old, $new, $fieldvalue); $x=$fieldvalue; } if (!in_array($fieldvalue, $echoed)) { //value already echoed? echo $fieldvalue."<br>"; $echoed[]=$fieldvalue; //add value into array } } echo "<br>"; } Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 Now... what does not working meen? You really need to learn how to ask for help. Quote Link to comment Share on other sites More sharing options...
jd2007 Posted July 12, 2007 Author Share Posted July 12, 2007 u see...i don't want the same result displayed twice...so i added $fieldvalue into an array $echoed...then the if statement checks if the value $fieldvalue is not in $echoed...if it is not, $fieldvalue is displayed...if it is , $fieldvalue doesn't get displayed... Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 If you dont want the same result displayed twice you need to adjust your query. Don't do any more work in PHP than you have to, databases are made for this sort of thing. $query = "SELECT Searching.page_title, Searching.info, Searching.link FROM Searching WHERE info LIKE'%".$name[$j]."%' GROUP BY info"; Quote Link to comment Share on other sites More sharing options...
jd2007 Posted July 12, 2007 Author Share Posted July 12, 2007 it still doesn't work... Quote Link to comment Share on other sites More sharing options...
trq Posted July 12, 2007 Share Posted July 12, 2007 Read the howto.... link in my signiture. Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 12, 2007 Share Posted July 12, 2007 Maybe you check your code and ensure you changed it. And I hope you got the right file open. Quote Link to comment Share on other sites More sharing options...
jd2007 Posted July 13, 2007 Author Share Posted July 13, 2007 hi Lumio, it worked now, thanks. the problem was i did not add this sign [] next to $echoed when the array was created ! thank u so much ! i put the code like this : $echoed[] = array(); while ($record = mysql_fetch_assoc($result)) { while (list($fieldname, $fieldvalue) = each ($record)) { $x=$fieldvalue; for ($l=0; $l<count($name); $l++) { $old = array($name[$l]); $new = array("<b><u>".$name[$l]."</u></b>"); $fieldvalue = str_replace($old, $new, $fieldvalue); } if (!in_array($x, $echoed)) { echo $fieldvalue."<br>"; $echoed[]=$x; } else if (!in_array($x, $echoed)) { exit; } else {} } } Quote Link to comment Share on other sites More sharing options...
Lumio Posted July 13, 2007 Share Posted July 13, 2007 please replace the first $echoed[] = array(); to $echoed = array(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.