preserving checkboxes across the pages in php

Hi I have php pagination program which  retrieves one page at a time from the database. First column is
checkboxes. How to preserve their value when users moves to the next page (GET requess)

The records per page are optional so it can be very long. So I am interested in doing it in the most efficient way
 
RozamundaAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Use session_start() at the top of all of the scripts involved.

Copy the checkbox information into the $_SESSION array and recover the checkbox information from the $_SESSION array.

As you do this, be aware that unchecked checkboxes do not appear in the request at all - they are not present and empty like an empty text input control; they are simply missing from the request.

Does that help answer the question?
0
 
Ray PaseurCommented:
Thanks for the points and best of luck with it, ~Ray
0
 
Slick812Commented:
greetings. Rozamunda, , using a next page as a GET request may be a problem, as far as I know there is no info gathered from the check boxes and sent with the page request , as usually  
index.php?page=2
so you may have to use javascript and get all of the checked boxes and send that with the page request. maybe liike -
index.php?page=2&check=0-2-3-5-7-11-14-23-24-31

this uses a numerical listing of the checked boxes , and when you do the call in PHP to rewrite the checkboxes, from a database array, you check and see if the numerical position of the checkbox is in the returned array and write 'checked' in the <input> tag for the checkbox

$check = $_GET['check'];
$checkAry = explode('-', $check);

for ($- = 0; $i < $ckCount; ++$i) {
// other code here to write <input type=checkbox +other stuff
if (in_array("".$i, $checkAry)) echo "checked";
}

but your methods may not allow this, it's hard to know what you may be doing in your code, from the info you have given so far. . . .
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Ray PaseurCommented:
@Slick812:  I had in mind something like this.  All the best, ~Ray
<?php // RAY_checkboxes_session.php
error_reporting(E_ALL);
session_start();

// INITIALIZE SOME TEST DATA - THIS MIGHT COME FROM A DATA BASE
if (!isset($_SESSION['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_SESSION['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_SESSION['box3'])) $_SESSION['box3'] = FALSE;

// PROCESS THE POST DATA
if ( isset($_GET['box1'])) $_SESSION['box1'] = TRUE;
if ( isset($_GET['box2'])) $_SESSION['box2'] = TRUE;
if ( isset($_GET['box3'])) $_SESSION['box3'] = TRUE;

if (!isset($_GET['box1'])) $_SESSION['box1'] = FALSE;
if (!isset($_GET['box2'])) $_SESSION['box2'] = FALSE;
if (!isset($_GET['box3'])) $_SESSION['box3'] = FALSE;

// TELL WHAT WAS CHECKED
if ($_SESSION['box1']) echo 'YOU CHECKED box1<br/>';
if ($_SESSION['box2']) echo 'YOU CHECKED box2<br/>';
if ($_SESSION['box3']) echo 'YOU CHECKED box3<br/>';

// CREATE THE FORM
$form = <<<EOFORM
<form method="get">
CHECK OR UNCHECK SOME BOXES HERE<br/>
<input name=box1 type="checkbox" /><br/>
<input name=box2 type="checkbox" /><br/>
<input name=box3 type="checkbox" /><br/>
<input type="submit" value="go" />
</form>
EOFORM;

// REPLACE THE FORM FIELDS TO INDICATE THE CHECKED VALUES IN THE SESSION
if ($_SESSION['box1']) $form = str_replace('box1', 'box1 checked', $form);
if ($_SESSION['box2']) $form = str_replace('box2', 'box2 checked', $form);
if ($_SESSION['box3']) $form = str_replace('box3', 'box3 checked', $form);

// PRESENT THE FORM
echo $form;

Open in new window

0
 
Slick812Commented:
thanks for your time Ray, but as it's seems to be the usual thing, you or I do not have enough knowledge of what the methods or problems may be. LOL, if only I had taken up mind reading instead of computer coding, I did not mean to post after question was answered, but I started the comment while I was eating lunch (free time I have to use  here), and posted after eating lunch,
0
 
RozamundaAuthor Commented:
Hi Slick, thanks for you reply

I am going to use jquery to read through checkobixes and put selected items into the array.
_GET I need to go to the next page, and then when I return I can check if any of the items
were selected by looking into my array of selected items which I am going to store in the _SESSION

The problem I can possible have is what if user selects on the page like  10000 items ( I have the option to
store pages that long). Can I just store them in the _Session ?
0
 
Ray PaseurCommented:
You can store any number of things in the Session array.  Or you can store them in a data base, which might be a smarter design, since a client who makes 10,000 selections, only to find that the session timed out and the data was lost will not be a client any more!
0
 
Slick812Commented:
@ Rozamunda
I hope you can take my next statements as an effort to help and not to criticize your methods.
I also think that a session can store that amount of data.
But people are not computers, and to have more than 200 (or even 100) displayed form data entry inputs (checkboxes, text, radios, selections) is more than I would want to deal with, or even could deal with, and have a clear Idea of what I was doing and remembering what the different data sets were for. I can not imagine being presented with 500 form inputs, much less 10000. Maybe you could have them process One page of 200 check boxes at a time? But if this does not apply for your users, then do not comment about it, and talk about any other help you might need.

While ray prefers a database storage, I usually go for a file storage, for a non "lookup or indexed" info store, , or an SQLite file database when only accessed from one page on the site. I think the default for session data, is to be stored in a file in the temp directory. The PHP serialize() function makes array storage in a file relativity easy. But there are advantages and disadvantages to both.
0
All Courses

From novice to tech pro — start learning today.