-1

I'm trying to do 2 things with my search script.

One i want to know how i could add a if statement to say if not results found echo "no results".

And secondly at the moment i limit results to 5, but i want the user to be able to click view more results and if they have been searching for users in 'London' then once they click view all results this will open in a new window and have all the results for their search displayed in the new window?

Can anyone please help me, have been working on this for some time and am still learning php and mysql.

<?php
//PHP CODE STARTS HERE

if(isset($_GET['submit'])){

// Change the fields below as per the requirements
$db_host="###";
$db_username="###";
$db_password="";
$db_name="###";
$db_tb_atr_name="display_name";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_GET['query']);


$query_for_result=mysql_query("SELECT *
                        FROM ptb_stats
                        WHERE display_name like '%".$query."%' OR location LIKE '%".$query."%' OR hobbies LIKE '%".$query."%' OR nationality LIKE '%".$query."%' OR gender LIKE '%".$query."%' OR local_station LIKE '%".$query."%' LIMIT 5");
echo "<div class=\"search-results\">";
while($data_fetch=mysql_fetch_array($query_for_result))
{

    echo "<div class=\"text\"><a href=\"profile.php?id={$data_fetch['user_id']}\" class=\"search\">";
    echo "<div class=\"spacing\"><img width=35px height= 30px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\" class=\"boxgridsearch\"/> "; 
     echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</a></div></div>";

}
echo "<div class=\"morebutton-search\"><a href=\"search_escorts.php?to=echo '%".$query."%'\" target=\"_blank\" \">+ view more results</a></div>";


mysql_close();
}

?>
4

1 回答 1

0

For your first part you could check with mysql_num_rows if 0 rows were returned. Note that mysql_ is deprecated though and you should be moving on to mysqli or PDO.

if(mysql_num_rows($query_for_result)==0){
    echo 'No Results';
}else{
    //process code
}    

For your second part it sounds as if you are looking for pagination. You would have a GET variable with the page number and based on that return the correct result set. You could have another GET called place with something like LONDON in. Then your query could be loaded in another page with the correct data displayed.

于 2012-12-29T22:32:02.137 回答