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`  
  `BOOK_NAME` varchar(100) NOT NULL,  
 -- Dumping data for table `books_tbl`  
 (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.

 $con = mysql_connect('localhost','root','');  
 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"]  
      if (isset($_GET["page"]))   
           $page = $_GET["page"];   
 $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  
 <head><title>Search option in a grid Tutorial |</title></head>  
 <form id="form1" method="POST" action="pagination.php?search">  
 <input type="text" name="search" /><input type="submit" id="btnsearch" name="btnsearch" value="Go" />  
 while ($row = mysql_fetch_array($rs_result)) {   
                     if ($_REQUEST["sid"] == $row['BOOK_ID']) // if your search result match row will be highlighted with yellow colour.  
                          echo "<tr style='background-color:yellow'>";  
                          echo "<tr>";  
       <td><?php echo $row['BOOK_ID']; ?></td>  
       <td><?php echo $row['BOOK_NAME']; ?></td>        
 $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  


  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

  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

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

  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

  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

  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

  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 .

  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

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

  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.

  11. This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.

    Digital Marketing Company in India
    Web Development Company in India
    Web Design Company in Chennai

  12. thanks for sharing the information.
    Indian Cyber Army’s most awaited internship is live now. Summer Internship 2018 on “ Ethical hacking” and book your seats before it runs out.Candidates have to get themselves registered to be a part of this Internship program. Here internship will give you on-the-job experience, help you learn whether you and Cyber security industry are a good match and can provide you with valuable connections and references. Here interns are usually exposed to a wide variety of tasks and responsibilities which allows the intern to showcase their strengths by working on projects for various managers that work on different parts of Indian Cyber Army. Becoming a high performing intern is a fantastic way to improve your employment prospects. This internship can be a great way to get your foot in the door of success with a prestigious or desirable Organization.As career in ethical hacking is most in demand.

  13. thanks for sharing the more valuable information.
    Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses. The training is conducted by subject specialist corporate professionals with wide experience in managing real-time ethical hacking/ cyber security projects. Indian Cyber Army implements a blend of academic learning and practical sessions to give the candidate optimum exposure.Ethical hacking training ,
    Ethical hacking course