2016-03-15 90 views
2

我有一個簡單的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

您可以使用PHP的empty()功能檢查,如果它是空的:當設置爲空時和false

if(empty($result)) { 
    echo "No results found"; 
} else { 
    // display your table 
} 

empty()將返回true

+0

It works,Thanks .. –

+0

@ArchanaP不客氣:) – Panda

1

您可以使用empty檢查$result有任何結果或不。

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

它顯示錯誤,代碼是否正確? –

+0

對不起,我在'$ result'後錯過了''',答案已更新。 – Egg