Jump to content

Store multiple selection from pagination info into single array


ktsirig
 Share

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.