<?
//spoof some data
//nowmally this would come from a database
$array = array(
array("id"=>0,"fruit"=>"apple"),
array("id"=>1,"fruit"=>"banana"),
array("id"=>2,"fruit"=>"coconut"),
array("id"=>3,"fruit"=>"damson"),
array("id"=>4,"fruit"=>"elderberry")
);
//generate the checkboxes
$cbox = "";
foreach ($array as $box){
$checked = (isset($_POST['cbox']) && in_array($box['id'],$_POST['cbox'])) ? 'checked="checked"' : "";
$cbox .= "<input type=\"checkbox\" name=\"cbox[]\" value=\"{$box['id']}\" $checked /> {$box['fruit']} <br/>";
}
//calculate the "score"
$score = isset($_POST['cbox'])
? "Your score is ". count($_POST['cbox']) ."<br/>"
: "";
//display the form
echo <<<EOL
$score
<form method="post" action="{$_SERVER['PHP_SELF']}">
<fieldset style="width:15%;">
$cbox
<input type="submit" name="submit" value="submit" />
</fieldset>
</form>
EOL;
?>