Jump to content

Loop input.


IwnfuM

Recommended Posts

hi , i have build an search engine .

i don't know really how to output the results.

i have done something like this .

 

$qry = mysql_fetch_assoc(mysql_query("SELECT `search2`FROM `search` WHERE `search1`='%".$search."%' "));
echo $qry['search2'];

 

but it output only 1 result , i wanna know to to output all the results he find.

thanks , Mor.

 

Link to comment
Share on other sites

You need to loop through the results if you know that there is going to be more than one result. Ie:-

 

//I find it handy to build the sql outside the function, this makes debugging easier!
$searchTerms = "SELECT `search2`FROM `search` WHERE `search1`='%".$search."%' ";
$sqlSearch = mysql_query($searchTerms);

//start loop here
while($qry = mysql_fetch_assoc($sqlSearch)){

//you could do this as a method of debugging also:-
//echo "<pre>";
//print_r($qry);
//echo "</pre>";

//print the results to screen
echo $qry['search2']."<br>";

}
//close the loop

 

Hope that helps a little anyway,

 

Cheers,

Rw

 

I wish I could type faster on some days!! What they said anyway!!

Link to comment
Share on other sites

mjdamato ,

you write it like that : '%{$search}%'

and I wrote it like that : '%".$search."%'

is there a difference ?

No difference. I don't like the look of code that concatenates variables how you had it. Besides, when using double quotes, the PHP parser will interpret variables in the string. If you are going to exit out of the string to concatenate a variable, why use double quotes. You can just put the variable inside the double quotes, but enclosing them in curly braces helps alleviate certain problems - such as using arrays.

 

and you'r code will print all the results that have founded?

how can i know how many have been found ?

 

The code I posted create a loop where YOU can insert the code for displaying the results. I don't know how you want to display them so I left it out. But, here is one example where each result is displayed on a separate line. I also added code to display the number of records returned.

$query = "SELECT `search2`FROM `search` WHERE `search1`='%{$search}%'";
$result = mysql_query($query);
if(mysql_num_rows($result)==0)
{
    echo "There were no results.";
}
else
{
    echo "There were " . mysql_num_rows($result) . " records found:<br />\n";
    while($row = mysql_fetch_assoc($result))
    {
        echo " - {$qry['search2']}<br />\n";;
    }
}

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.