I am using the following code to search in the site..I wish to display the message "no result found" when the query returns no result.What changings should i do in the code
The HTML is:
<div class="form-container">
<form method="get" action="search.php">
<input type="text" name="search" placeholder="Search keywords..."/>
<input type="submit" name="find" value="">
</form>
</div>
The PHP is:
<?php
if(isset($_GET['find'])){
$search = @$_GET['search'];
$query = "SELECT * FROM `me` WHERE `post_title` LIKE '%$search%'";
$run = mysqli_query($con,$query);
while($row=mysqli_fetch_assoc($run)):
$post_id = $row['post_id'];
$post_category = $row['post_category'];
$post_author = $row['post_author'];
$post_title = $row['post_title'];
$post_content = substr($row['post_content'],0,700);
$post_video = $row['post_video'];
$post_misc = $row['post_misc'];
$post_image = $row['post_image'];
$post_date = $row['post_date'];
?>
<a class="post_title" href="complete_post.php?complete1=<?php echo $post_id; ?>">
<?php echo $post_title; ?>
</a>
<p style="color:#949494; font-family:calibri; text-align:justify; margin-left:8px; width:700px; font-size:15pt; float:left;">
<span style="float:left; font-size:9pt; font-family:arial; color:#959595;">
By <b><?php echo $post_author; ?></b> on <b><?php echo $post_date; ?></b> in
<b><?php echo $post_category; ?></b>
</span>
</br>
<?php echo $post_content; ?>...
</p>
<a href="complete_post.php?complete=<?php echo $post_id; ?>" class="r-m">
Read more..
</a>
<?php endwhile; } ?>
Add an if
-condition before your while
-loop
$run = mysqli_query($con,$query);
if ( ! empty($run) && (! empty($_GET['search'])) ) {
while($row=mysqli_fetch_assoc($run)):
// your while code
endwhile;
} else {
echo "Nothing Found";
}
The num_rows function will return the number of rows fetched by the query. This should do it.
<?php
if(isset($_GET['find'])){
$search = @$_GET['search'];
$query = "SELECT * FROM `me` WHERE `post_title` LIKE '%$search%'";
$run = mysqli_query($con,$query);
if($run->num_rows != 0){
while($row=mysqli_fetch_assoc($run)):
$post_id = $row['post_id'];
$post_category = $row['post_category'];
$post_author = $row['post_author'];
$post_title = $row['post_title'];
$post_content = substr($row['post_content'],0,700);
$post_video = $row['post_video'];
$post_misc = $row['post_misc'];
$post_image = $row['post_image'];
$post_date = $row['post_date'];
?>
<a class="post_title" href="complete_post.php?complete1=<?php echo $post_id; ?>">
<?php echo $post_title; ?>
</a>
<p style="color:#949494; font-family:calibri; text-align:justify; margin-left:8px; width:700px; font-size:15pt; float:left;">
<span style="float:left; font-size:9pt; font-family:arial; color:#959595;">
By <b><?php echo $post_author; ?></b> on <b><?php echo $post_date; ?></b> in
<b><?php echo $post_category; ?></b>
</span>
</br>
<?php echo $post_content; ?>...
</p>
<a href="complete_post.php?complete=<?php echo $post_id; ?>" class="r-m">
Read more..
</a>
<?php endwhile;
} else { ?>
<p>There are no results to display.</p>
<?php }
} ?>