Demo of checkbox status connected to database
Here we will store values of the days ( in 1 to 7 ) by separating them by comma (,) in a table. We could have kept the weekdays in an array for simplicity but in a real project it is most likely that data will be stored in a table. So let us use one table to store weekdays and its number. Here in total we have used two tables, one stores the weekdays and its numbers (plus_weekdays) and the other table stores the userid and its selection of weekdays (plus_week). Here also we have used one record for all our tutorial and that is for userid smo. Now let us first collect the selection of userid smo stored in our table.$count=$dbo->prepare("SELECT days FROM plus_week WHERE userid='smo' ");
if($count->execute()){
//echo " Success <br>";
$row = $count->fetch(PDO::FETCH_OBJ);
}
Inside our days field we store the selection with day number separated by comma. For example we have kept Monday, Wednesday, Saturday as selection of userid smo then the field days will store value 1,3,6 . So let us try to collect this value and create an array.
$days_array=explode(",",$row->days);
You can understand that we have used explode() command to create an array which stores the day selection of userid smo. Now we will collect all the weekdays and its numbers from the table plus_weekdays and display then as checkboxes. Here before displaying we will do a small check to know whether this weekday number is there inside our $day_array or not by using in_array function. This is to know whether the weekday is previously selected by user or not. If selected previously then we will keep the checkbox checked by adding attribute checked inside the checkbox tag. For this we will use one variable $st which we will assign empty string or checked value by checking the presence inside array using in_array function. Here is the complete code.
$qt="SELECT * FROM plus_weekdays";
echo "<form method=post action=''><input type=hidden name=todo value=submit_form>";
echo "<table border='0' width='50%' cellspacing='0' cellpadding='0' align=center>";
$st="";
foreach ($dbo->query($qt) as $noticia) {
if(@$bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
if(in_array($noticia['day_no'],$days_array)){$st="checked";}
else{$st="";}
echo "<tr bgcolor='$bgcolor'>
<td class='data'><input type=checkbox name=days_array[] value='$noticia[day_no]' $st> $noticia[days]</td></tr>";
}
echo "</table>
<input type=submit value=update></form>
</center>";
@$todo=$_POST['todo']; // take care of register global if off
if(isset($todo) and $todo=="submit_form"){
$days_array=$_POST['days_array'];
$tag_string="";
while (list ($key,$val) = @each ($days_array)) {
//echo "$val,";
$tag_string.=$val.",";
}
$tag_string=substr($tag_string,0,(strLen($tag_string)-1));// remove the last , from string
$sql=$dbo->prepare("UPDATE plus_week SET days=:tag_string where userid='smo'");
$sql->bindParam(':tag_string',$tag_string,PDO::PARAM_STR);
if($sql->execute()){
echo "Data Updated<br>";
}
}else{
//print_r($sql->errorInfo()); // if any error is there it will be posted
}
The sql dump of the tables along with the config file ( for database connection ) and readme file is kept inside the ZIP file.