reenadak

live search via ajax

Sep 16th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <title>Live Search</title>
  6.     <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  7.     <style type="text/css">
  8.         img {
  9.             border-width: 0
  10.         }
  11.        
  12.         * {
  13.             font-family: 'Lucida Grande', sans-serif;
  14.         }
  15.     </style>
  16. </head>
  17.  
  18. <body>
  19.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  20.     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  21.  
  22.     <div class="row">
  23.         <div class="container">
  24.             <div class="form-group">
  25.                 <input type="text" class="form-control pull-right" style="width:20%" id="search" placeholder="Type to search table...">
  26.             </div>
  27.             <br>
  28.             <table class="table-bordered table pull-right" id="mytable" cellspacing="0" style="width: 100%;">
  29.                 <thead>
  30.                     <tr role="row">
  31.                         <th>Id</th>
  32.                         <th>Name</th>
  33.                         <th>Slug</th>
  34.                         <th>Name</th>
  35.                     </tr>
  36.                 </thead>
  37.                 <tbody>
  38.                     <?php
  39.  
  40.     $db = new mysqli("myhost", "dbuser", "mypassword", "mydbname");
  41.  
  42.     if($db->connect_error) die("Error in database connection - ".$db->connect_error);
  43.  
  44.     $result = $db->query("SELECT * FROM short_url ORDER BY rating DESC LIMIT 50 ");
  45.  
  46.     if($result->num_rows>0){
  47.  
  48.         while($row = $result->fetch_assoc() )
  49.         {
  50.             echo "
  51.            <tr>
  52.                <td>".$row['id']."</td>
  53.                <td>".$row['name']."</td>
  54.                <td><a href='".$row['slug']."'>".$row['slug']."</a></td>
  55.                <td>".$row['rating']."</td>
  56.            </tr>";
  57.         }
  58.     }
  59.     else        
  60.     {
  61.         echo "0 results";
  62.     }
  63.  
  64.   //  $conn->close();
  65.  
  66.     ?>
  67.                 </tbody>
  68.             </table>
  69.         </div>
  70.     </div>
  71.     <script>
  72.         // Write on keyup event of keyword input element
  73.         $(document).ready(function () {
  74.             $("#search").keyup(function () {
  75.                 _this = this;
  76.                 // Show only matching TR, hide rest of them
  77.                 $.each($("#mytable tbody tr"), function () {
  78.                     if ($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) === -1)
  79.                         $(this).hide();
  80.                     else
  81.                         $(this).show();
  82.                 });
  83.             });
  84.         });
  85.     </script>
  86. </body>
  87.  
  88. </html>
Add Comment
Please, Sign In to add comment