PHP使用高级搜索显示从数据库到网站的表

时间:2017-05-12 07:24:50

标签: java php mysqli

我制作的程序只包含一个搜索栏,因此我无法过滤更准确或相关的表格。

我还需要一个搜索栏才能在两个搜索字段中输入值,点击发布后会从数据库中搜索并获得最多相关的一次。

<?php

    if(isset($_POST['search']))
   {
     $valueToSearch = $_POST['valueToSearch'];
  // search in all table columns
 // using concat mysql function
 $query = "SELECT * FROM `included` WHERE CONCAT(`id`, `a`, `b`, 
 `c`,`c`,`d`,`e`,`f`,`g`,`h`,`i`) LIKE '%".$valueToSearch."%'";
   $search_result = filterTable($query);

  }
  else {
     $query = "SELECT * FROM `included`";
 $search_result = filterTable($query);
}

// function to connect and execute the query
 function filterTable($query)
 {
  $connect = mysqli_connect("localhost", "root", "", "hospitaldata");
  $filter_Result = mysqli_query($connect, $query);
 return $filter_Result;
 }

 ?>

      <!DOCTYPE html>
 <html>
 <head>
 <img src="nop.jpg">
 <title>PHP HTML TABLE DATA SEARCH</title>

     <style>
  table,tr,th,td{
   border:.3px solid blue;
 color:#000;
   font-family:sans-serif;
  }

div.relative {
 position: relative;
 top: -50px;
 width: 1400px;
 height: 100px;
     color: #0C3;
 font-family: "Arial Black", Gadget, sans-serif;
font-size: 24px;
} 

div.absolute {
 position: absolute;
top: 51px;
 right: 20;
 width: 1261px;
 height: 40px;
 color: #999;
 font-family: Verdana, Geneva, sans-serif;
 left: 65px;
  }
  input[type=text] {
 alignment-baseline:central;
 width: 130px;
 box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px; 
  background-color: white;
  background-image:url('ds.jpg'); 


  padding: 12px 20px 12px 40px;
  -webkit-transition: width 0.4s ease-in-out;
  transition: width 0.4s ease-in-out;
 }

 input[type=text]:focus {
width: 50%;
 }
 table,tr,th,tr
 {
 border:1px solid blue;

 }

  </style>
  </style>
  </head>
  <body>
  <div class="relative"><h1 align="center">HOSPITAL</h1>
  <div class="absolute" align="center">Check provided points here</div>
  </div>

  <form action="index.php" method="post">
    <input type="text" name="valueToSearch" placeholder="Search..."><br> 

    <input type="submit" name="search" value=">>"><br><br>

    <table>
        <tr>

  <th>Building</th>
 <th>Floor</th>
  <th>zone</th>
  <th>Room no</th>
  <th>Room Type</th>
 <th>Room Name</th>
    <th>Types of Connection</th>
          <th>Suggested</th>
    <th>Provided</th>
        </tr>

 <!-- populate table from mysql database -->
         <?php while($row = mysqli_fetch_array($search_result)):?>
        <tr>

  <td><?php echo $row['a'];?></td>
   <td><?php echo $row['b'];?></td>
   <td><?php echo $row['c'];?></td>
   <td><?php echo $row['d'];?></td>
   <td><?php echo $row['e'];?></td>
   <td><?php echo $row['f'];?></td>
   <td><?php echo $row['g'];?></td>
   <td><?php echo $row['h'];?></td>
    <td><?php echo $row['i'];?></td>
        </tr>
        <?php endwhile;?>
    </table>
 </form>

 </body>
 </html>

1 个答案:

答案 0 :(得分:0)

根据您的评论我理解以下内容:

  • 您有一个带有搜索输入字段的网页
  • 您想要第二个输入,以便您的用户可以搜索更多主持人
  • 一个serach按钮应该发送两个输入字段

一个非常简单(和未经测试)的版本可能如下所示:

<form action="search.php" method="post">
  <label><input name="search1"/> First keyword</label>
  <label><input name="search2"/> Second keyword</label>
  <input type="submit" value="Serach"/>
</form>
<?php
// If the Search button was pressed
if(isset($_POST['search1'])) {
  $stmt = $pdo->prepare($query = "SELECT * FROM `included` WHERE CONCAT(`id`, `a`, `b`, `c`) LIKE '%:search1%' OR CONCAT(`d`, `e`) LIKE '%:search2%'");
  $stmt->execute(['search1' => $POST['search1'], 'search2' => $POST['search2'] :? 'default');
  foreach ($stmt as $row) {
    // do something with $row
  }
}

当然,您必须根据自己的需要调整SQL语句。

有关如何设置$pdo数据库连接的信息,请参阅链接:http://stackoverflow.com/a/60496/1152471