我有一个简单的SQL搜索和显示结果脚本。 是在stackoverflow成员的帮助下完成的。 我想显示“未找到结果:如果搜索结果为空。 我是编码的初学者,如果有人能帮助我,那就太棒了。 脚本安装在http://http://klkerala.tk/pincode.php中 搜索结果使表的标题可见,甚至是空的。 我想隐藏它并显示“没有找到结果”而是...... 提前致谢
<?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 style="text-align:right;margin:0px 0px 0px;">
</div>
<div id="toys-grid">
<form name="frmSearch" method="post" action="pincode.php">
<div class="search-box">
<p><input type="text" placeholder="PIN CODE" name="search[name]" class="demoInputBox" value="<?php echo $name; ?>" />or<input type="text" placeholder="Location" name="search[code]" class="demoInputBox" value="<?php echo $code; ?>" /><input type="submit" name="go" class="btnSearch" value="Search"><input type="reset" class="btnSearch" 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>
答案 0 :(得分:1)
您可以使用empty检查$result
是否有任何结果。
<?php
if (empty($result)) {
echo "<p>No results matched the query</p>\n";
} else {
?>
<table>
...
</table>
<?php
}
?>
答案 1 :(得分:0)
您可以使用PHP的empty()
函数来检查它是否为空:
if(empty($result)) {
echo "No results found";
} else {
// display your table
}
empty()
将在空时返回true
,并在设置时返回false
。