Re: Retaining checkbox values through pages
| From: | Richard Lynch | Date: | Sun, 25 Aug 2002 00:35:25 +0000 |
| Subject: | Re: Retaining checkbox values through pages | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-114014@lists.php.net to get a copy of this message | ||
>I have a script that displays a list of names from a database that can be
>added into a mailing list by checking the checkbox next to the name.
>
>The names are listed at 10 names per page, with links to the next or
>previous 10 names.
>
>let's say i check some names on page one, then i move on to page two and
>check some names there. is there a way to remember these names as i browse
>thru the pages? that is to say, is there a way to remember who i checked on
>page one if i come back to it again?
Sure.
<FORM ACTION=<?=$PHP_SELF?> METHOD=POST>
<?php
$allnames =
array('Alice','Bob','Charlie','Derek','Ed','Francis','George','Harry','Izzy'
);
if (!isset($selected)){
$selected = array();
}
if (!isset($start)){
$start = 0;
}
$limit = 3;
for ($i = $start; $i < $start + $limit; $i++){
$checked = $selected[$i] ? 'CHECKED' : '';
echo "<INPUT TYPE=CHECKBOX NAME=selected[$i] $checked>
$allnames[$i]<BR>\n";
}
# Dump out any previously selected names not currently displayed:
for ($i = 0; $i < $start; $i++){
if (isset($selected[$i])){
echo "<INPUT TYPE=HIDDEN NAME=selected[$i]>\n";
}
}
for ($i = $start+$limit; $i < count($allnames); $i++){
if (isset($selected[$i])){
echo "<INPUT TYPE=HIDDEN NAME=selected[$i]>\n";
}
}
?>
</FORM>
<A HREF=<?=$PHP_SELF?>?start=<?=max(0, $start-$limit)?>>Prev</A>
<A HREF=<?=$PHP_SELF?>?start=<?=min(count($allnames),
$start+$limit)?>>Next</A>
--
Like Music? http://l-i-e.com/artists.htm