Jump to content

php help ?


jd2007

Recommended Posts

$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...

Link to comment
https://forums.phpfreaks.com/topic/59625-php-help/
Share on other sites

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>";      
	          
	          
         }
?>

Link to comment
https://forums.phpfreaks.com/topic/59625-php-help/#findComment-296266
Share on other sites

$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>";      
	          
	          
	          }

Link to comment
https://forums.phpfreaks.com/topic/59625-php-help/#findComment-296315
Share on other sites

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";

Link to comment
https://forums.phpfreaks.com/topic/59625-php-help/#findComment-296338
Share on other sites

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 {}
                  }
                       
                      
	          
	          
	          }

Link to comment
https://forums.phpfreaks.com/topic/59625-php-help/#findComment-296960
Share on other sites

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.