2016-07-20 5 views
0

Wenn ich etwas in das Suchfeld ein und drücken Sie Suche schreiben, sollte es nur angepasst Zeilen zurück und verstecken anderen Reihen.Return nur gesucht Zeilen in der Tabelle und verbergen andere

Hier ist mein Code, es funktioniert vervollkommnet einziges Problem ist es mir Rekordliste + alle Datensatz der Tabelle gesucht gibt. Was kann ich tun, um nur gesuchte Daten in der Tabelle anzuzeigen?

<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> 

Antwort

0

halten nur einzelne while-Schleife und die Abfrage ausführen und Abfrage suchen, wie gezeigt unten, damit er das Problem lösen:

Ja
<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

! Es funktioniert, aber nur Problem ist, dass ich diesen Clingy entfernen muss! rote Farbe vom Hintergrund! Danke Man ~ –

+0

vielleicht ein Design-Problem, Sie müssen das Stylesheet teilen, um Ihnen zu helfen. Welchen Hintergrund willst du nicht div oder Tisch? – Ravistm

+0

tat ich. Keine Probleme. Ich habe geschrieben ** echo ""; ** das. Ich habe es auf null geändert. –

Verwandte Themen