2015-11-24 56 views
2

如何運行搜索功能?我不能顯示任何結果與此代碼,我不知道如何處理這個類和功能..我是一個新手這種代碼..請檢查我的代碼....謝謝!帶有班級和公共職能的PHP搜索

<?php 
$txtsearch = $_POST['txtsearch']; 

class BlogController{ 

    public function search($txtsearch){ 
     $mysqli = new mysqli("localhost","root","","sample_db"); 

     $display_query = "SELECT * FROM `tb_blogs` WHERE id='$txtsearch' "; 
     $result = $mysqli->query($display_query); 
    } 

} 

    if(isset($_POST["btnsearch"])){ 
     echo BlogController::search(); 
    } 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
<body> 
    <a href="create.php"> Create New Blog</a><br> 
    <form action="function.php" method="post"> 
     <input text-align:right type="text" name="txtsearch" placeholder="search"> 
     <input type="submit" name="btnsearch" value="Search"> 

    <table border="1" width="60%" cellpadding="2" cellspacing="0"> 
     <thead> 
      <tr> 
       <th width="5%">ID</th> 
       <th width="20%">Title</th> 
       <th width="20%">Author</th> 
       <th width="40%">Content</th> 
       <th width="15%">Action</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php while($blog = $result->fetch_object()): ?> 
      <tr> 
       <td><?php echo $blog->id?></td> 
       <td><?php echo $blog->title?></td> 
       <td><?php echo $blog->author?></td> 
       <td><?php echo $blog->content?></td> 
       <td> 
        <a href="edit.php?blog_id=<?php echo $blog->id;?>">Edit</a> 
        <a class ="btn_del" href="delete.php?blog_id=<?php echo $blog->id;?>">Delete</a> 
       </td> 
      </tr> 
     <?php endwhile?> 
     </tbody> 

    </table> 

<script type="text/javascript"> 
    $(function(){ 
     $(".remove-btn").on('click',function(){ 

      var is_confirm = confirm("Do you want to delete record?"); 

      if(is_confirm){ 
       window.location = $(this).attr('href'); 
      } 

      return false; 
     }); 

    }); 
</script> 
</form> 
</body> 
</html> 

回答

0

試試這個:

class BlogController{ 

     public $txtsearch; 

     function __construct($search) { 
      $this->$txtsearch = $search; 
     } 

     public function search(){ 
      $mysqli = new mysqli("localhost","root","","sample_db"); 

      $display_query = "SELECT * FROM `tb_blogs` WHERE id='$this->$txtsearch' "; 
      $result = $mysqli->query($display_query); 
      return $result; 
     } 

} 

創建一個實例,然後調用函數搜索

$myclass = new BlogController($_POST['txtsearch']); //pass value to the construct function 
print_r($myclass->search()); // will return an array 
+0

這是行不通的,先生 – light

+0

請回聲查詢並檢查它是否是工作 –

+0

對不起先生..但仍然... doesn't工作 – light