2017-02-16 62 views
0

我在我的測試數據庫中有6個項目。搜索框位於http://vince.netau.net在我甚至在搜索框中搜索任何內容之前,數據庫中的所有6個項目都顯示出來。我需要在代碼中更改哪些內容,以便只顯示搜索框而沒有任何結果?謝謝搜索PHP - 如何調出搜索框沒有結果

<?php 
define("ROW_PER_PAGE",2); 
require_once('db.php'); 
?> 
<html> 
<head> 
<style> 
body{width:615px;font-family:arial;letter-spacing:1px;line-height:20px;} 
.tbl-qa{width: 100%;font-size:0.9em;background-color: #f5f5f5;} 
.tbl-qa th.table-header {padding: 5px;text-align: left;padding:10px;} 
.tbl-qa .table-row td {padding:10px;background-color: #FDFDFD;vertical-align:top;} 
.button_link {color:#FFF;text-decoration:none; background-color:#428a8e;padding:10px;} 
#keyword{border: #CCC 1px solid; border-radius: 4px; padding: 7px;background:url("demo-search-icon.png") no-repeat center right 7px;} 
.btn-page{margin-right:10px;padding:5px 10px; border: #CCC 1px solid; background:#FFF; border-radius:4px;cursor:pointer;} 
.btn-page:hover{background:#F0F0F0;} 
.btn-page.current{background:#F0F0F0;} 
</style> 
</head> 
<body> 
<?php 
    $search_keyword = ''; 
    if(!empty($_POST['search']['keyword'])) { 
     $search_keyword = $_POST['search']['keyword']; 
    } 
    $sql = 'SELECT * FROM posts WHERE post_title LIKE :keyword OR description LIKE :keyword OR post_at LIKE :keyword ORDER BY id DESC '; 

    /* Pagination Code starts */ 
    $per_page_html = ''; 
    $page = 1; 
    $start=0; 
    if(!empty($_POST["page"])) { 
     $page = $_POST["page"]; 
     $start=($page-1) * ROW_PER_PAGE; 
    } 
    $limit=" limit " . $start . "," . ROW_PER_PAGE; 
    $pagination_statement = $pdo_conn->prepare($sql); 
    $pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR); 
    $pagination_statement->execute(); 

    $row_count = $pagination_statement->rowCount(); 
    if(!empty($row_count)){ 
     $per_page_html .= "<div style='text-align:center;margin:20px 0px;'>"; 
     $page_count=ceil($row_count/ROW_PER_PAGE); 
     if($page_count>1) { 
      for($i=1;$i<=$page_count;$i++){ 
       if($i==$page){ 
        $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />'; 
       } else { 
        $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />'; 
       } 
      } 
     } 
     $per_page_html .= "</div>"; 
    } 

    $query = $sql.$limit; 
    $pdo_statement = $pdo_conn->prepare($query); 
    $pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR); 
    $pdo_statement->execute(); 
    $result = $pdo_statement->fetchAll(); 
?> 
<form name='frmSearch' action='' method='post'> 
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div> 
<table class='tbl-qa'> 
    <thead> 
    <tr> 
     <th class='table-header' width='20%'>Title</th> 
     <th class='table-header' width='40%'>Description</th> 
     <th class='table-header' width='20%'>Date</th> 
    </tr> 
    </thead> 
    <tbody id='table-body'> 
    <?php 
    if(!empty($result)) { 
     foreach($result as $row) { 
    ?> 
     <tr class='table-row'> 
     <td><?php echo $row['post_title']; ?></td> 
     <td><a href="<?php echo $row['description']; ?>" 
       <td><?php echo $row['description']; ?></td> 
       <td><?php echo $row['post_at']; ?></td> 
     </tr> 
    <?php 
     } 
    } 
    ?> 
    </tbody> 
</table> 
<?php echo $per_page_html; ?> 
</form> 
</body> 
</html> 
+0

我應該改變這裏的代碼 – user3444610

回答

0

爲了簡化芯片的答案..你只需要讓你的第一個IF控制所有的搜索和顯示的代碼......

$search_keyword = $_POST['search']['keyword']; 
    } << remove this closing curly bracket 

    and put it here 
    ....... 
    $result = $pdo_statement->fetchAll(); 
} // END if $_POST['search']['keyword'] is !empty 
?> 
+0

真的很困惑。我是否應該將3行代碼更改爲您提供的1行代碼,並移除大括號並將其放在$ result = $ pdo_statement-> fetchAll()之前。如果是的話,它給了我一個錯誤? – user3444610

+0

在代碼中移動一個'}',否則你的代碼是相同的。將'$ search_keyword = $ _POST ['search'] ['keyword'];'移動到$ result = $下面pdo_statement->使用fetchall();' –

1

你必須將所有的數據庫php代碼移動到第一個if語句。你正在檢查表單是否已經提交,但是你仍然運行查詢。通過將所有代碼移動到if語句中,您將阻止查詢運行,除非表單實際上已提交。您已經在檢查您的結果是否爲空,因此結果未定義即可。

$search_keyword = ''; 
    if(!empty($_POST['search']['keyword'])) { 
     $search_keyword = $_POST['search']['keyword']; 
$sql = 'SELECT * FROM posts WHERE post_title LIKE :keyword OR description LIKE :keyword OR post_at LIKE :keyword ORDER BY id DESC '; 

    /* Pagination Code starts */ 
    $per_page_html = ''; 
    $page = 1; 
    $start=0; 
    if(!empty($_POST["page"])) { 
     $page = $_POST["page"]; 
     $start=($page-1) * ROW_PER_PAGE; 
    } 
    $limit=" limit " . $start . "," . ROW_PER_PAGE; 
    $pagination_statement = $pdo_conn->prepare($sql); 
    $pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR); 
    $pagination_statement->execute(); 

    $row_count = $pagination_statement->rowCount(); 
    if(!empty($row_count)){ 
     $per_page_html .= "<div style='text-align:center;margin:20px 0px;'>"; 
     $page_count=ceil($row_count/ROW_PER_PAGE); 
     if($page_count>1) { 
      for($i=1;$i<=$page_count;$i++){ 
       if($i==$page){ 
        $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />'; 
       } else { 
        $per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />'; 
       } 
      } 
     } 
     $per_page_html .= "</div>"; 
    } 

    $query = $sql.$limit; 
    $pdo_statement = $pdo_conn->prepare($query); 
    $pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR); 
    $pdo_statement->execute(); 
    $result = $pdo_statement->fetchAll(); 
    } 
+0

新手該塊所以不完全確定你的意思。這是我在我的db.php中的所有內容<?php \t $ database_username =''; \t $ database_password =''; \t $ pdo_conn = new PDO('; dbname =',$,$); ?> – user3444610