Keeping track of the checkboxes selected across the page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aniltc
Forum Newbie
Posts: 17
Joined: Thu Nov 16, 2006 6:42 am

Keeping track of the checkboxes selected across the page?

Post by aniltc »

I have a list of users(signed for a newsletter).I'm retrieving the list from a database when the admin clicks on a button.
There are maximum 10 users diplayed per page.For example,if I had 30 users in the database,I would have 3 pages of users(Page 1,2,3);
For each listed user ,I have a checkbox on the right side.The checkbox is for when the admin wants to selected a few people to whom send the newsletter.
Ex:
Page: 1|2|3
1.ana checkbox
2.john checkbox
3.mike checkbox
---------------------
10.cindy checkbox
Then,if the admin want to go to another page :
Page : 1|2|3
1.lucy checkbox
2.shinoda checkbox
-------------------------
10.michelle checkbox
and so on
Down this list of users , the admin has 2 button : Send and Sendall
The Send button is used when he wants to send the newsletter only to the people with the checkbox selected

My problem is when the admin wants to select people from different pages(Ex: he selects 2 people from the first page,3 on the second ,1 person on the last page , and then he clicks send)
Because when he clicks the button for another page,the data is lost,so it's hard to keep evidence of the total checkboxes selected
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You should be able to save all the information in a sesison. The question is how to "store" and how to "process". You need to let the user determine the action somehow. The other potential problem is ensuring that the 'links' for the pages actually store checked boxes. These could for instance be made into submit buttons with border etc switched off by CSS.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

An alternative way of doing it would be to display the list of users with their checkboxes from a database and place them in a scrolable DIV (vertically of course).

Outside the DIV at the bottom you could have your send buttons.

Store the selected users as an array and simply loop that array when the user wants to action something.
aniltc
Forum Newbie
Posts: 17
Joined: Thu Nov 16, 2006 6:42 am

Post by aniltc »

Hey,

Code below handle pagination and tracking checkbox value, but it is not working. How can i proceed with this code.

Code: Select all

<?php
ob_start();
echo "<form name=form1 method=post name=f1 onSubmit>";
require "config.php";           // All database details will be included here 

$page_name="eg.php"; //  If you use this code with a different page ( or file ) name then change this 

if(!isset($_GET['start'])) {                         // This variable is set to zero for the first page
$start = 0;
}
$ck='';
$ck=@implode(',',$_POST['chkname']);
echo $ck;
$eu = ($_GET['start'] - 0); 
$limit = 10;                                 // No of records to be shown per page.
$this1 = $eu + $limit; 
$back = $eu - $limit; 
$next = $eu + $limit; 


/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=" SELECT * FROM student  ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////

/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo "<TABLE width=50% align=center  cellpadding=0 cellspacing=0> <tr>";
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Name</font></td>";
echo "<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Class</font></td>";
echo "<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Mark</font></td></tr>";

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
echo " SELECT * FROM student  limit $eu, $limit ";
$query=" SELECT * FROM student  limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();

//////////////// Now we will display the returned records in side the rows of the table/////////
while($noticia = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}

echo "<tr >";
echo "<td align=left> <input type='checkbox' name='chkname[{$noticia['id']}]' value='$noticia[id]'> </td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[name]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[class]</font></td>"; 
echo "<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[mark]</font></td>"; 

echo "</tr>";
}
echo "</table>";
////////////////////////////// End of displaying the table with records ////////////////////////

/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0) { 
print "<a href='$page_name?start=$back' onClick=check()><font face='Verdana' size='2'>PREV</font></a>"; 
} 
//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for($i=0;$i < $nume;$i=$i+$limit){
if($i <> $eu){
echo " <a href='$page_name?start=$i&ck=$ck'onClick='check()';><font face='Verdana' size='2'>$l</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$l</font>";}        /// Current page is not displayed as link and given font color red
$l=$l+1;
}

  echo""; 
     


echo "</td><td  align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume) { 
print "<a href='$page_name?start=$next' ><font face='Verdana' size='2'>NEXT</font></a>";} 
echo "<input type='submit' value='s' >";
echo "</td></tr></table>";
echo "</form>";
?>
Post Reply