Appreciate the help. I think I just about understand that.
Please explain this if possible:
mktime(0, 0, 0, $i, 1, 2011));
also in that code, wouldn’t $checked = ($i == $monthResult[‘month’]) ? ’ checked=“checked”’ : ‘’; be checking the same month number from the sql result each time?
Wouldn;t the whole of that piece of code need to be run for every line in the set of mysql results?
Brilliant. Yeh, I did wonder. Thanks for actually adding that extra bit. I’m sure I could ahve figured it out myself but appreciate the advice. I’ll see what happens now adding to my application!
<?php
if (isset($birdResult['bird'])) {
$birdName = $birdResult['bird'];
} else {
$birdName = null;
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<label for="bird">Bird:</label>
<input type="text" name="bird" value="<?php echo $birdName; ?>">
<label>Months Seen:</label>
<?php
//Make new array
$checkedMonths = array();
if (isset($monthResult)) {
//While a new row in the SQL results available...
while($monthResult = mysql_fetch_array($result2, MYSQL_ASSOC)){
//Add that month number to the checkedMonths array
$checkedMonths[] = $monthResult['month'];
}
}
//For 12 times...
for($i=1; $i <= 12; $i++) {
//If the current loop number (1-12) is in the checkedMonths array then set checked to this string otherwise leave blank
$checked = (in_array($i, $checkedMonths)) ? ' checked="checked"' : '';
//get the text version of the month of the timestamp given
$monthText = date('F', mktime(0, 0, 0, $i, 1, 2011));
//print out the input checkbox tag with the value as a month number and the text month after it
echo '<input type="checkbox" name="months[]" value="'.$i.'"'.$checked.' />'.$monthText."\
";
}
?>
<input type="submit" name="submit" value="Add Bird">
</form>
I add an item with all 12 months checked. They show up in the database with all 12 months. When I clcik my edit button to see the form and expect all the checkboxes to be checked for every month, they are all checked except the first one.
It seems like it is not checking the first selected box of the ones selected. So for example when adding an item (in my case a “bird”) if i checked march, april and may, When I come to edit that item, only april and may are checked.
Any thoughts on what could be doing this strange behavior.