ktsirig Posted May 4, 2012 Share Posted May 4, 2012 Hi I have a results page from a MySQL query and I have implemented pagination to present data in chunks of 25 per page in a table view (each row => 1 entry). My problem is that next to each data row, I have a selection checkbox, and if the user clicks on it, he can download the selected entries. This thing worked OK before I employed paginated view, when I presented all data in one page. Now, if I select e.g. 2 entries in page 1, then I jump to page 12 and select another 2 entries, if I click on the "Retrieve" button, I get only the last 2 entries (from the last page visited). How can I solve this problem? With JS? Can I use PHP only? Quote Link to comment https://forums.phpfreaks.com/topic/262065-store-multiple-selection-from-pagination-info-into-single-array/ Share on other sites More sharing options...
scootstah Posted May 4, 2012 Share Posted May 4, 2012 One way would be using AJAX to add the checked boxes to a SESSION, or temporary cookie. Quote Link to comment https://forums.phpfreaks.com/topic/262065-store-multiple-selection-from-pagination-info-into-single-array/#findComment-1342976 Share on other sites More sharing options...
Mahngiel Posted May 4, 2012 Share Posted May 4, 2012 One way would be using AJAX to add the checked boxes to a SESSION, or temporary cookie. +1 there. When you changed the page, you left the scope. Quote Link to comment https://forums.phpfreaks.com/topic/262065-store-multiple-selection-from-pagination-info-into-single-array/#findComment-1343001 Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2012 Share Posted May 4, 2012 The only pure html/php way would be if the pagination 'links' are actually form submit buttons and you are actually submitting the check box data on one page to another page when you click on a pagination button. Quote Link to comment https://forums.phpfreaks.com/topic/262065-store-multiple-selection-from-pagination-info-into-single-array/#findComment-1343043 Share on other sites More sharing options...
scootstah Posted May 4, 2012 Share Posted May 4, 2012 The only pure html/php way would be if the pagination 'links' are actually form submit buttons and you are actually submitting the check box data on one page to another page when you click on a pagination button. Which would quickly become annoying for the user if they try to go back a page. Quote Link to comment https://forums.phpfreaks.com/topic/262065-store-multiple-selection-from-pagination-info-into-single-array/#findComment-1343061 Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2012 Share Posted May 4, 2012 Only if you don't clear the post data. See following example, based on the phpfreaks pagination tutorial - <?php session_start(); // database connection info $conn = mysql_connect('localhost','dbusername','dbpassword') or trigger_error("SQL", E_USER_ERROR); mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM numbers"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); list($numrows) = mysql_fetch_row($result); // number of rows to show per page $rowsperpage = 25; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $newpage = (int)$_GET['currentpage']; } else { // default page num $newpage = 1; } // end if // modify the requested page based on any pagination form buttons, set/clear any remembered session checkbox data $page = isset($_POST['page']) ? trim($_POST['page']) : false; if($page){ // values of <<, <, x, >, or >> switch($page){ case '<<': $newpage = 1; break; case '<': $newpage--; break; case '>': $newpage++; break; case '>>': $newpage = $totalpages; break; default: // not one of the symbols, should be a number $page = intval($page); if($page > 0){ $newpage = $page; } break; } // set or clear the state of the check boxes in $_SESSION['choice'] // $_SESSION['ids'] = array of ids on the page that submitted foreach($_SESSION['ids'] as $key){ if(isset($_POST['choice'][$key])){ $_SESSION['choice'][$key] = 1; } elseif (isset($_SESSION['choice'][$key])) { unset($_SESSION['choice'][$key]); } } header("location: {$_SERVER['SCRIPT_NAME']}?currentpage=$newpage"); // clear post data exit; } // if current page is greater than total pages... if ($newpage > $totalpages) { // set current page to last page $newpage = $totalpages; } // end if // if current page is less than first page... if ($newpage < 1) { // set current page to first page $newpage = 1; } // end if /****** build the pagination links ******/ // I moved this up in the code so that the links are built first so they can be output anywhere in the content // range of num links to show $range = 3; $links = ''; // if not on page 1, don't show back links if ($newpage > 1) { // show << link to go back to page 1 $links .= "<input type='submit' name='page' value='<<'>"; // show < link to go back 1 page $links .= "<input type='submit' name='page' value='<'>"; } // end if // loop to show links to range of pages around current page for ($x = ($newpage - $range); $x < (($newpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $newpage) { // 'highlight' it but don't make a link $links .= " [<b>$x</b>] "; // if not current page... } else { $links .= "<input type='submit' name='page' value='$x'>"; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($newpage != $totalpages) { // echo forward link for next page $links .= "<input type='submit' name='page' value='>'>"; // echo forward link for lastpage $links .= "<input type='submit' name='page' value='>>'>"; } // end if /****** end build pagination links ******/ /****** get the actual database content for the requested page ******/ // the offset of the list, based on current page $offset = ($newpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); echo "<form action='?currentpage=$newpage' method='post'>"; echo $links . '<br />'; // show the pagination buttons // while there are rows to be fetched... $ids_this_page = array(); // a list of the checkbox id's on this page while ($list = mysql_fetch_assoc($result)) { // echo data $checked = isset($_SESSION['choice'][$list['id']]) ? " checked='checked'" : ''; echo "{$list['id']}:{$list['number']} <input type='checkbox' name='choice[{$list['id']}]' value='1'$checked><br />"; $ids_this_page[] = $list['id']; } // end while $_SESSION['ids'] = $ids_this_page; echo "<input type='submit' name='submit' value='Retrieve'>"; echo "<br />"; echo $links . '<br />'; // show the pagination buttons echo "</form>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262065-store-multiple-selection-from-pagination-info-into-single-array/#findComment-1343103 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.