Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “如何显示”;“未找到结果”;在搜索结果中是否为空?_Php_Sql_Search - Fatal编程技术网

Php “如何显示”;“未找到结果”;在搜索结果中是否为空?

Php “如何显示”;“未找到结果”;在搜索结果中是否为空?,php,sql,search,Php,Sql,Search,我有一个简单的SQL搜索和显示结果脚本。 是在stackoverflow成员的帮助下完成的。 如果搜索结果为空,我想显示“未找到任何结果”。 我是一个初学者在编码,如果有人可以帮助我在这方面,这将是伟大的。 脚本安装在 搜索结果使表的标题可见,即使是空的。 我想隐藏它并显示“找不到结果”。。。。 提前谢谢 <?php require_once("perpage.php"); require_once("dbcontroller.php"); $db_

我有一个简单的SQL搜索和显示结果脚本。 是在stackoverflow成员的帮助下完成的。 如果搜索结果为空,我想显示“未找到任何结果”。 我是一个初学者在编码,如果有人可以帮助我在这方面,这将是伟大的。 脚本安装在 搜索结果使表的标题可见,即使是空的。 我想隐藏它并显示“找不到结果”。。。。 提前谢谢

    <?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>

带有搜索和分页的PHP CRUD
您可以使用检查
$result
是否有任何结果

<?php
if (empty($result)) {
   echo "<p>No results matched the query</p>\n";
} else {
?>
    <table>
    ...
    </table>
<?php
}
?>

...

您可以使用PHP的
empty()
函数检查它是否为空:

if(empty($result)) {
    echo "No results found";
} else {
    // display your table
}
empty()
将在设置为空时返回
true
,设置为空时返回
false

对不起,我错过了
$result
之后,答案将更新。