infolink

Thursday, 21 April 2016

Search option in gridview in php

Search option in a grid view in php
Search Option
This tutorial is about search option in a grid. There are many searching techniques. Here I explain how to add search option in a grid when a grid has also server side pagination. It’s a simple and very easy way that I use.
When you using server side pagination first you must check it out that on which page your record is exist. Like in my example I want to search ID number “17”. And on per page I am displaying total 10 numbers of rows/records so it means ID number “17” exist on page 2.

Copy and paste this example code which is more understandable.

Step 1:

Create database like "tutorials" and then create table "BOOKS_TBL" with more then 10 record. below is the script of table and data.



 --  
 -- Table structure for table `books_tbl`  
 --  
 CREATE TABLE IF NOT EXISTS `books_tbl` (  
  `BOOK_ID` int(11) NOT NULL AUTO_INCREMENT,  
  `BOOK_NAME` varchar(100) NOT NULL,  
  PRIMARY KEY (`BOOK_ID`),  
  UNIQUE KEY `BOOK_NAME` (`BOOK_NAME`)  
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=33 ;  
 --  
 -- Dumping data for table `books_tbl`  
 --  
 INSERT INTO `books_tbl` (`BOOK_ID`, `BOOK_NAME`) VALUES  
 (10, 'Biology'),  
 (11, 'Book 1'),  
 (20, 'Book 10'),  
 (21, 'Book 11'),  
 (22, 'Book 12'),  
 (23, 'Book 13'),  
 (24, 'Book 14'),  
 (25, 'Book 15'),  
 (26, 'Book 16'),  
 (27, 'Book 17'),  
 (28, 'Book 18'),  
 (29, 'Book 19'),  
 (12, 'Book 2'),  
 (30, 'Book 20'),  
 (31, 'Book 21'),  
 (32, 'Book 22'),  
 (13, 'Book 3'),  
 (14, 'Book 4'),  
 (15, 'Book 5'),  
 (16, 'Book 6'),  
 (17, 'Book 7'),  
 (18, 'Book 8'),  
 (19, 'Book 9'),  
 (9, 'Chemistry'),  
 (1, 'English A'),  
 (2, 'English B'),  
 (7, 'General Science'),  
 (5, 'Mathematics'),  
 (8, 'Physics'),  
 (6, 'Social Studies'),  
 (3, 'Urdu A'),  
 (4, 'Urdu B');  

Step 2:

create a file with the name of "pagination.php". and insert the following Code in this file.

 <?php   
 error_reporting(0);  
 $num_rec_per_page=10;  
 $con = mysql_connect('localhost','root','');  
 mysql_select_db('tutorials');  
 if (isset($_GET["search"])) // this if is related to search option  
 {  
      $searchid = $_POST["search"]; // assig the search id which you want to search to $searchid variable.  
      $pageid = ceil($searchid / $num_rec_per_page); // this statement is checking that on which page this search record is exist.   
      header("location: pagination.php?sid=".$searchid."&page=".$pageid); // after check the page redirect the URL on that page like in my case URL will: http://localhost/tutorials/pagination.php?sid=17&page=2  
 } // end if of $_GET["search"]  
 else  
 {  
      if (isset($_GET["page"]))   
      {   
           $page = $_GET["page"];   
      }   
      else   
      {   
           $page=1;  
      }  
 }  
 $start_from = ($page - 1) * $num_rec_per_page;  
 $sql = "SELECT * FROM BOOKS_TBL order by BOOK_ID LIMIT $start_from, $num_rec_per_page";   
 //echo $sql; exit;  
 $rs_result = mysql_query ($sql); //run the query  
 ?>   
 <html>  
 <head><title>Search option in a grid Tutorial | www.developerqueries.blogspot.com</title></head>  
 <body>  
 <form id="form1" method="POST" action="pagination.php?search">  
 <input type="text" name="search" /><input type="submit" id="btnsearch" name="btnsearch" value="Go" />  
 </form>  
 <table>  
 <tr><td>ID</td><td>Name</td></tr>  
 <?php   
 while ($row = mysql_fetch_array($rs_result)) {   
 ?>   
       <?php   
                     if ($_REQUEST["sid"] == $row['BOOK_ID']) // if your search result match row will be highlighted with yellow colour.  
                     {  
                          echo "<tr style='background-color:yellow'>";  
                     }  
                     else  
                     {  
                          echo "<tr>";  
                     }  
                ?>  
       <td><?php echo $row['BOOK_ID']; ?></td>  
       <td><?php echo $row['BOOK_NAME']; ?></td>        
       </tr>  
 <?php   
 }  
 ?>   
 </table>  
 <?php   
 $sql = "SELECT * FROM BOOKS_TBL";   
 $rs_result = mysql_query($sql); //run the query  
 $total_records = mysql_num_rows($rs_result); //count number of records  
 $total_pages = ceil($total_records / $num_rec_per_page);   
 echo "<a href='pagination.php?page=1'>".'|<'."</a> "; // Goto 1st page   
 for ($i=1; $i<=$total_pages; $i++) {   
       echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";   
 }  
 echo "<a href='pagination.php?page=$total_pages'>".'>|'."</a> "; // Goto last page  
 ?>  
 </body>  
 </html>  

12 comments:


  1. the blog is very interesting and will be much useful for us. thank you for sharing the blog with us. please keep on updating.
    Informatica Training in Chennai

    ReplyDelete
  2. Thanks for this blog. All screenshots can explain the full blogs easily. This is very easy to understand. Using this screen shots know the details becomes easy.
    PPC Services Chennai

    ReplyDelete
  3. Thanks for sharing such a great information..Its really nice and informative.
    PHP Training in Chennai

    ReplyDelete
  4. your information is really awesome as well as it is very excellent and i got more interesting information from your blog. Android App Development Company in Chennai

    ReplyDelete
  5. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Sat Coaching Chennai

    ReplyDelete
  6. This is extremely helpful info!! Very good work. It is very interesting to learn and easy to understood. Thank you for giving information. Please let us know and more information get post to link.

    Fresher Jobs in Chennai
    Fresher Jobs in Delhi
    Fresher Jobs in Bangalore
    Fresher Jobs in Kolkata

    ReplyDelete
  7. This was a fantastic Blog. Really loved reading your blog post. Very informative and helpful. Guys if you if learn want to learn PHP join best php training in pune .

    ReplyDelete
  8. Very nice post here thanks for it I always like and search such topics and everything connected to them. Keep update more information..
    Digital Marketing Company in Chennai

    ReplyDelete
  9. superb.....blog is much interesting about search option in grid view which engaged me more.Spend a worthful time.keep updating more.
    SEO Company in India

    ReplyDelete
  10. Hello, thank you very much for this article this is really helpful to website development Gold Coast. I find it useful with my future plans. Once again, thank you very much.

    http://rent-a-website.com.au/rental-plans

    ReplyDelete