php - How to show "no results found" in the search result is empty?
I have a simple SQL search and show result script. Did it with the help of stackoverflow members. I want to show "No results found: if the search result is empty. I am a beginner in coding , if anyone can help me in this , it will be great. Script is installed in http://http://klkerala.tk/pincode.php The search result have the table's header visible , even its empty. I want to hide it and display "no results found" instead.... Thanks in advance
<?php
require_once("perpage.php");
require_once("dbcontroller.php");
$db_handle = new DBController();
$name = "";
$code = "";
$queryCondition = "";
if(!empty($_POST["search"])) {
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {
$queryCases = array("name","code");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "name":
$name = $v;
$queryCondition .= "name LIKE '" . $v . "%'";
break;
case "code":
$code = $v;
$queryCondition .= "code LIKE '" . $v . "%'";
break;
}
}
}
}
$orderby = " ORDER BY id desc";
$sql = "SELECT * FROM pincodes " . $queryCondition;
$href = 'pincode.php';
$perPage = 20;
$page = 1;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$query = $sql . $orderby . " limit " . $start . "," . $perPage;
$result = $db_handle->runQuery($query);
if(!empty($result)) {
$result["perpage"] = showperpage($sql, $perPage, $href);
}
?>
<html>
<head>
<title>PHP CRUD with Search and Pagination</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h2></h2>
<div >
</div>
<div id="toys-grid">
<form name="frmSearch" method="post" action="pincode.php">
<div >
<p><input type="text" placeholder="PIN CODE" name="search[name]" value="<?php echo $name; ?>" />or<input type="text" placeholder="Location" name="search[code]" value="<?php echo $code; ?>" /><input type="submit" name="go" value="Search"><input type="reset" value="Reset" onclick="window.location='pincode.php'"></p>
</div>
<table cellpadding="10" cellspacing="1">
<thead>
<tr>
<th><strong>PIN CODE</strong></th>
<th><strong>Location</strong></th>
<th><strong>City</strong></th>
<th><strong>State</strong></th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $k=>$v) {
if(is_numeric($k)) {
?>
<tr>
<td><?php echo $result[$k]["name"]; ?></td>
<td><?php echo $result[$k]["code"]; ?></td>
<td><?php echo $result[$k]["category"]; ?></td>
<td><?php echo $result[$k]["price"]; ?></td>
</tr>
<?php
}
}
if(isset($result["perpage"])) {
?>
<tr>
<td colspan="6" align=right> <?php echo $result["perpage"]; ?></td>
</tr>
<?php } ?>
<tbody>
</table>
</form>
</div>
</body>
</html>
Answer
Solution:
You can use empty to check if
$result
has any results or not.Answer
Solution:
You can use PHP's
empty()
function to check if it's empty:empty()
will returntrue
when empty andfalse
when set.