mythri Posted January 31, 2015 Share Posted January 31, 2015 I have more than 1000 records in my database and while displaying i use Pagination with search criteria. Actually When i give search criteria, its displays defined number of records from database, defined in the same file thru pagination. It shows 2,3,4... number with links. But when i click on 2nd page , it goes to 2nd page but again we need to provide search criteria. I wan this to display without search criteria. My code is like this <?php if(isset($_GET['page'])){ $page=$_GET['page']; } else { $page=1; } $start_page=($page-1)*20; if(isset($_POST['search'])) { $stud_id=$_POST['stud_id']; $stud_name=$_POST['stud_name']; $class=$_POST['class']; $section=$_POST['section']; $session=$_POST['session']; if(isset($_POST['search']) && ($_POST['stud_id']!='')) { $sql="select * from student where enroll_no LIKE '".$stud_id."%' "; } elseif(isset($_POST['search']) && ($_POST['stud_name']!='')) { $sql="select * from student where stud_name LIKE '".$stud_name."%' "; } elseif(isset($_POST['search']) && ($_POST['class']!='') && ($_POST['section']!='')) { $sql="select * from student where class LIKE '".$class."%' AND section LIKE '".$section."%' "; } elseif(isset($_POST['search']) && ($_POST['class']!=='')) { echo "Please select section along with class"; } elseif(isset($_POST['search'])) { $sql="select * from student limit $start_page,20"; } } $query=mysql_query($sql); ?> <div class="container"> <div class="row-fluid"> <div class="span12"> <div class="w-box"> <div class="w-box-header"> <h4>Student List</h4> </div> <div class="w-box-content"> <form method="post" action="" > <div class="formSep"><input type="text" name="stud_id" id="stud_id" value="" placeholder="Enroll No" /> <input type="text" name="stud_name" id="stud_name" value="" placeholder="Student Name"/> <select name="class" id="class"> <option value=""> CLASS </option> <option value="Nursery">Nursery</option> <option value="LKG">LKG</option> <option value="UKG">UKG</option> <option value="I">I</option> <option value="II">II</option> <option value="III">III</option> <option value="IV">IV</option> <option value="V">V</option> <option value="VI">VI</option> <option value="VII">VII</option> <option value="VIII">VIII</option> <option value="IX">IX</option> <option value="X">X</option> <option value="XI">XI</option> <option value="XII">XII</option> </select> <select name="section" id="section"><option value=""> SECTION </option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select> <input type="submit" name="search" id="search" value="Search" class="btn btn-info" /> </div> </form> </div> </div> <div class="w-box w-box-blue"> <div class="w-box-header"> <h4>Student List</h4> </div> <div class="w-box-content"> <?php $count=mysql_num_rows($query); if($count==0) { echo "<h2>SORRY NONE OF THE RECORD FOUND IN TABLE</h2>"; } else { echo "<table id='dt_hScroll' class='table table-striped'>"; echo "<thead><tr><th>Enroll No</th><th>Student Name</th><th>Photo</th><th>Class</th><th>Class Teacher</th><th>Section</th><th>Emergency Contact</th></tr> </thead>"; $i=0; while($row=mysql_fetch_array($query)) { if($i == 1) { echo '<tr class="EvenTableRows">'; $i=0; } else { echo '<tr class="OddTableRows">'; $i = 1; } ?> <td><?php echo $row['enroll_no']; ?></td> <td><a href="edit_student.php?enroll_no=<?php echo $row['enroll_no']; ?>"><?php echo $row['stud_name']; ?></a></td> <td><img src="<?php echo '../'.$row['stud_photo']; ?>" width="50px" height="50px" /></td> <td><?php echo $row['class']; ?></td> <td> <?php } } ?> </tbody></table> </div> </div> <?php $sql1="select COUNT(id) from student"; $rs_result = mysql_query($sql1); $row1 = mysql_fetch_row($rs_result); $total_records = $row1[0]; $total_pages = ceil($total_records / 20); if ($page > $total_pages) { // set current page to last page $page = $total_pages; } // end if // if current page is less than first page... if ($page < 1) { // set current page to first page $page = 1; } // end if $range=4; echo "<div style='float:left; width:200px;text-align:center; margin-left:40%;'>"; // echo "<a href='view_student.php?page=".$i."'>".$i."</a> "; if ($page > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?page=1'>First</a> "; // get previous page num $prevpage = $page - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>Previous</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($page - $range); $x < (($page + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $total_pages)) { // if we're on current page... if ($x == $page) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($page != $total_pages) { // get next page $nextpage = $page + 1; // echo forward link for next page echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next</a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?page=$total_pages'>Last</a> "; } // end if echo "</div>"; ?> how can i make it , please suggest Quote Link to comment https://forums.phpfreaks.com/topic/294292-pagination-with-search-result/ Share on other sites More sharing options...
mac_gyver Posted January 31, 2015 Share Posted January 31, 2015 you will need to pass the search term as part of the pagination links. if you do an advanced search on the forum (the white star/snowflake thing to the right of the search box) and search for http_build_query and my username as the 'find author', you will find a number of examples showing how to combine search terms and pagination links. http_build_query() lets you build the query string part of url's based on an array of data (the $_GET array can be used as the input array to the function.) this lets each separate part of your code, the search and the pagination, to manage just those parts of the array they are responsible for and the links will have the combined result from all your code. Quote Link to comment https://forums.phpfreaks.com/topic/294292-pagination-with-search-result/#findComment-1504456 Share on other sites More sharing options...
QuickOldCar Posted January 31, 2015 Share Posted January 31, 2015 (edited) One thing I did notice is you are using both GET and POST for this All your variables could be held all in a GET request and are an array which http_build_query() could use A trick here is to unset $_GET['page'] from the array and make it what you desire. Besides the approach here building arrays, you could also unset get and manually append either page=* or &page=* to the ends, but I feel letting http_build_query do it is a cleaner approach. A demo for you to try minus your actual queries. <?php $total_pages = 40; //dummy data if (empty($_GET) || !isset($_GET['page']) || !ctype_digit(trim($_GET['page'])) || trim($_GET['page']) <= 0) { $_GET['page'] = 1; $current_page = 1; } else { $current_page = trim($_GET['page']); } $prev_page = $current_page - 1; $next_page = $current_page + 1; //showing your build query echo http_build_query($_GET, '', '&'); //set array $nav_array = array(); //back to first page if ($prev_page >= 2) { $_GET['page'] = 1; $nav_array[] = "<a style='text-decoration:none;' href='?" . http_build_query($_GET, '', '&') . "'>1...</a> "; } //previous page $_GET['page'] = $prev_page; if ($prev_page >= 1) { $nav_array[] = "<a style='text-decoration:none;' href='?" . http_build_query($_GET, '', '&') . "'>$prev_page</a> "; } //current page $_GET['page'] = $current_page; $nav_array[] = "<a style='text-decoration:none;font-weight: bold;' href='?" . http_build_query($_GET, '', '&') . "'>$current_page</a> "; //next page $_GET['page'] = $next_page; if ($next_page <= $total_pages) { $nav_array[] = "<a style='text-decoration:none;' href='?" . http_build_query($_GET, '', '&') . "'>$next_page</a>"; } //last page if ($next_page < $total_pages) { $_GET['page'] = $total_pages; $nav_array[] = "<a style='text-decoration:none;' href='?" . http_build_query($_GET, '', '&') . "'>...$total_pages</a> "; } ?> <form action="" method="GET"> <div class="formSep"><input type="text" name="stud_id" id="stud_id" value="" placeholder="Enroll No" /> <input type="text" name="stud_name" id="stud_name" value="" placeholder="Student Name"/> <select name="class" id="class"> <option value=""> CLASS </option> <option value="Nursery">Nursery</option> <option value="LKG">LKG</option> <option value="UKG">UKG</option> <option value="I">I</option> <option value="II">II</option> <option value="III">III</option> <option value="IV">IV</option> <option value="V">V</option> <option value="VI">VI</option> <option value="VII">VII</option> <option value="VIII">VIII</option> <option value="IX">IX</option> <option value="X">X</option> <option value="XI">XI</option> <option value="XII">XII</option> </select> <select name="section" id="section"><option value=""> SECTION </option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> <option value="D">D</option> </select> <input type="submit" name="search" id="search" value="Search" class="btn btn-info" /> </div> </form> <?php foreach ($nav_array as $nav) { echo "$nav "; } ?> Edited January 31, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/294292-pagination-with-search-result/#findComment-1504472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.