Jump to content

PHP- How to display “no results” message when no results have been found


hgkhkhk

Recommended Posts

I have built a search engine for my booking system. It works properly but the only issue I cannot resolve is how to display a "no results" message if nothing has been found in the system.

 

I tried putting an }else{ echo 'no results'; } at the end the very end of the code, but when results are found, it still displays the "no results" message. Does anyone know where I can insert this line to ensure that "no results" does not show up when results actually turn up and to ensure it only shows, when no actual results have been found? Thank you.

 
<?php

$server="localhost";
$username="root";
$password="";
$dbname="equipment";
$conn=mysqli_connect($server,$username,$password,$dbname);

$server="localhost";
$username="root";
$password="";
$dbname="qmb_ground_floor";
$conn2=mysqli_connect($server,$username,$password,$dbname);

$server="localhost";
$username="root";
$password="";
$dbname="qmb_level_01";
$conn3=mysqli_connect($server,$username,$password,$dbname);

$server="localhost";
$username="root";
$password="";
$dbname="qmb_level_02";
$conn4=mysqli_connect($server,$username,$password,$dbname);
?>

<!doctype html>

<html>

<head> 

<link rel="stylesheet" type="text/css" href="resultsstyle.css">
<link rel="icon" href="Icon.jpg">

<title>Search</title>

</head>

<body>

<div class="header">

<img class="icon" src="Icon.jpg"> 
<div class="search">Search </div>

</div>

<div class="select">Results </div>



<?php



if (isset($_POST['submit-search'])){
$search=mysqli_real_escape_string($conn,$_POST['search']);
$sql="SELECT * FROM bookings WHERE equipment LIKE'%$search%' OR start_time LIKE '%$search%'OR end_time LIKE '%$search%'OR selected_date LIKE '%$search%'";
$result=mysqli_query($conn, $sql);
$queryResult=mysqli_num_rows($result);

if(empty($_POST['search'])){
echo '<span class="empty">Search box was empty. Please try again.</span>';
}else{



if($queryResult>0){
while($row=mysqli_fetch_assoc($result)){
echo "
<span class='e'>Equipment: <span class='first'>".htmlspecialchars($row['equipment'])."</span></span><br>
<span class='bd'>Booking Date: <span class='second'>".htmlspecialchars($row['selected_date'])."</span></span><br>
<span class='st'>Start Time: <span class='third'>".htmlspecialchars($row['start_time'])."</span></span><br>
<span class='et'>End Time: <span class='fourth'>".htmlspecialchars($row['end_time'])."</span></span><br>
<span class='n'>Notes: <span class='fifth'>".htmlspecialchars($row['notes'])."</span></span><br><br><br><br>
";
}
}

$search=mysqli_real_escape_string($conn2,$_POST['search']);
$sql="SELECT * FROM bookings2 WHERE room LIKE'%$search%' OR start_time LIKE '%$search%'OR end_time LIKE '%$search%'OR selected_date LIKE '%$search%'";
$result=mysqli_query($conn2, $sql);
$queryResult=mysqli_num_rows($result);



if($queryResult>0){
while($row=mysqli_fetch_assoc($result)){
echo "
<span class='e'>Room: <span class='first'>".htmlspecialchars($row['room'])."</span></span><br>
<span class='bd'>Booking Date: <span class='second'>".htmlspecialchars($row['selected_date'])."</span></span><br>
<span class='st'>Start Time: <span class='third'>".htmlspecialchars($row['start_time'])."</span></span><br>
<span class='et'>End Time: <span class='fourth'>".htmlspecialchars($row['end_time'])."</span></span><br>
<span class='n'>Notes: <span class='fifth'>".htmlspecialchars($row['notes'])."</span></span><br><br><br><br>
";
}
}

$search=mysqli_real_escape_string($conn3,$_POST['search']);
$sql="SELECT * FROM bookings3 WHERE room LIKE'%$search%' OR start_time LIKE '%$search%'OR end_time LIKE '%$search%'OR selected_date LIKE '%$search%'";
$result=mysqli_query($conn3, $sql);
$queryResult=mysqli_num_rows($result);



if($queryResult>0){
while($row=mysqli_fetch_assoc($result)){
echo "
<span class='e'>Room: <span class='first'>".htmlspecialchars($row['room'])."</span></span><br>
<span class='bd'>Booking Date: <span class='second'>".htmlspecialchars($row['selected_date'])."</span></span><br>
<span class='st'>Start Time: <span class='third'>".htmlspecialchars($row['start_time'])."</span></span><br>
<span class='et'>End Time: <span class='fourth'>".htmlspecialchars($row['end_time'])."</span></span><br>
<span class='n'>Notes: <span class='fifth'>".htmlspecialchars($row['notes'])."</span></span><br><br><br><br>
";
}


}

$search=mysqli_real_escape_string($conn4,$_POST['search']);
$sql="SELECT * FROM bookings4 WHERE room LIKE'%$search%' OR start_time LIKE '%$search%'OR end_time LIKE '%$search%'OR selected_date LIKE '%$search%'";
$result=mysqli_query($conn4, $sql);
$queryResult=mysqli_num_rows($result);


if($queryResult>0){
while($row=mysqli_fetch_assoc($result)){
echo "
<span class='e'>Room: <span class='first'>".htmlspecialchars($row['room'])."</span></span><br>
<span class='bd'>Booking Date: <span class='second'>".htmlspecialchars($row['selected_date'])."</span></span><br>
<span class='st'>Start Time: <span class='third'>".htmlspecialchars($row['start_time'])."</span></span><br>
<span class='et'>End Time: <span class='fourth'>".htmlspecialchars($row['end_time'])."</span></span><br>
<span class='n'>Notes: <span class='fifth'>".htmlspecialchars($row['notes'])."</span></span><br><br><br><br>
";
}
}


}
}






?>





</div>

</body>

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