2016-07-20 2 views
0

検索ボックスに何かを書き込んで検索すると、一致した行のみが返され、他の行は非表示になります。検索された行のみを表と非表示で戻す

ここは私のコードですが、それは完璧な問題ですが、それは私に検索されたレコード+テーブルのすべてのレコードリストを与えてくれます。 検索したデータのみを表に表示するにはどうすればよいですか?

<div id="pageContent"><br /> 
    <div class="search" align="right"> 
     <form action="" method="post"> 
      Search: <input type="text" name="term" /><br /> 
      <input type="submit" value="Submit" /> 
     </form> 
    </div> 
    <div class="container"> 
     <table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover"> 
    <?php 
     include_once '../storescripts/connect_to_mysql.php'; 
     $num_rec_per_page=5; 
     if (isset($_GET["page"])) 
     { 
      $page = $_GET["page"]; 
     } 
     else 
     { 
      $page=1; 
     } 
     $start_from = ($page-1) * $num_rec_per_page; 
     $result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page"); 
    ?> 
      <thead> 
       <tr class="success"> 
        <th>Id</th> 
        <th>Product Name</th> 
        <th>Price</th> 
        <th>Status</th> 
        <th>Quantity</th> 
        <th>Details</th> 
        <th>Category</th> 
        <th>Subcategory</th> 
        <th>Date Added</th> 
        <th colspan="2">Functions</th> 
       </tr> 
      </thead> 
    <?php 
     if (!empty($_REQUEST['term'])) { 
      $term = mysql_real_escape_string($_REQUEST['term']);  

      $sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'"; 

      $r_query = mysql_query($sql); 
      if($r_query>1) 
      { 
       while ($row = mysql_fetch_array($r_query)){ 
        echo "<tr bgcolor='red'>"; 
        echo "<td>".$row['id']."</td>";     
        echo "<td>".$row['product_name']."</td>"; 
        echo "<td>".$row['price']."</td>"; 
        echo "<td>".$row['status']."</td>"; 
        echo "<td>".$row['quantity']."</td>"; 
        echo "<td>".$row['details']."</td>"; 
        echo "<td>".$row['category']."</td>"; 
        echo "<td>".$row['subcategory']."</td>"; 
        echo "<td>".$row['date_added']."</td>"; 
        echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>"; 
        echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>"; 
        echo "</tr>"; 
       } 
      } 
      else{ 
       echo "Nothing should be displayed"; 
      } 
     } 
    ?> 
    <?php 
     while($row=mysql_fetch_array($result)) 
     { 
      echo "<tr class='danger'>"; 
      echo "<td>".$row['id']."</td>"; 
      echo "<td>".$row['product_name']."</td>"; 
      echo "<td>".$row['price']."</td>"; 
      echo "<td>".$row['status']."</td>"; 
      echo "<td>".$row['quantity']."</td>"; 
      echo "<td>".$row['details']."</td>"; 
      echo "<td>".$row['category']."</td>"; 
      echo "<td>".$row['subcategory']."</td>"; 
      echo "<td>".$row['date_added']."</td>"; 
      echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>"; 
      echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>"; 
      echo "</tr>"; 
     } 
    ?> 
     </table> 

答えて

0

だけでループしながら、単一維持し、その下に示すように、クエリと検索クエリを実行するには、問題を解決します:

<div id="pageContent"><br /> 
    <div class="search" align="right"> 
     <form action="" method="post"> 

      Search: <input type="text" name="term" /><br /> 
      <input type="submit" value="Submit" /> 
     </form> 
    </div> 
    <div class="container"> 
     <table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover"> 
      <?php 
      include_once '../storescripts/connect_to_mysql.php'; 
      $num_rec_per_page = 5; 
      if (isset($_GET["page"])) { 
       $page = $_GET["page"]; 
      } else { 
       $page = 1; 
      }; 
      $start_from = ($page - 1) * $num_rec_per_page; 
      $sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page"; 
      ?> 
      <thead> 
       <tr class="success"> 
        <th>Id</th> 
        <th>Product Name</th> 
        <th>Price</th> 
        <th>Status</th> 
        <th>Quantity</th> 
        <th>Details</th> 
        <th>Category</th> 
        <th>Subcategory</th> 
        <th>Date Added</th> 
        <th colspan="2">Functions</th> 
       </tr> 
      </thead> 


<?php 
if (!empty($_REQUEST['term'])) { 

    $term = mysql_real_escape_string($_REQUEST['term']); 

    $sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'"; 
} 
$r_query = mysql_query($sql); 
if ($r_query > 1) { 

    while ($row = mysql_fetch_array($r_query)) { 
     echo "<tr bgcolor='red'>"; 
     echo "<td>" . $row['id'] . "</td>"; 
     echo "<td>" . $row['product_name'] . "</td>"; 
     echo "<td>" . $row['price'] . "</td>"; 
     echo "<td>" . $row['status'] . "</td>"; 
     echo "<td>" . $row['quantity'] . "</td>"; 
     echo "<td>" . $row['details'] . "</td>"; 
     echo "<td>" . $row['category'] . "</td>"; 
     echo "<td>" . $row['subcategory'] . "</td>"; 
     echo "<td>" . $row['date_added'] . "</td>"; 
     echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>"; 
     echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>"; 



     echo "</tr>"; 
    } 
} else { 
    echo "Nothing should be displayed"; 
} 
?> 
     </table> 
+0

はい!それは働いているが問題はClingyを取り除く必要があることだけだ!背景からの赤い色!おかげで男〜 –

+0

多分いくつかのデザインの問題、あなたはあなたを助けるためにスタイルシートを共有する必要があります。あなたはdivのテーブルやテーブルをしたくない背景? – Ravistm

+0

しました。問題ありません。私はwriiten ** echo ""; **これを持っています。私はそれをnullに変更しました。 –

関連する問題