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
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
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
Share on other sites

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

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.