SQL Result

Hey,

I I had the following as the result of a query:

here’s my code:

		$sql2 = "SELECT month FROM seen_month WHERE bird_id = $bird_id";
		$result2 = mysql_query($sql2) or die(mysql_error());
		$monthResult = mysql_fetch_assoc($result2);

I’m just a bit confused how I can actually use this data.

normally when I do a query, I get 1 row of data with a number of fields.

however in this scenario I get 1 field with a number of rows.

I can write:

$monthResult['month';]

to get the first item as in “1” but not sure how to get to the other 3 rows.

Any help would be appreciated.

Thanks

Neil

Use the following lines of code.

$sql2 = "SELECT month FROM seen_month WHERE bird_id = $bird_id";
$result2 = mysql_query($sql2) or die(mysql_error());
while($monthResult = mysql_fetch_array($result2, MYSQL_ASSOC)){
//Process data
echo $monthResult['month'];
}

Thanks,

Just another question. I have a form like so:

	<label>Months Seen:</label>
	<input type="checkbox" name="months[]" value="1">January
	<input type="checkbox" name="months[]" value="2">Febuary
	<input type="checkbox" name="months[]" value="3">March
	<input type="checkbox" name="months[]" value="4">April
	<input type="checkbox" name="months[]" value="5">May
	<input type="checkbox" name="months[]" value="6">June
	<input type="checkbox" name="months[]" value="7">July
	<input type="checkbox" name="months[]" value="8">August
	<input type="checkbox" name="months[]" value="9">September
	<input type="checkbox" name="months[]" value="10">October
	<input type="checkbox" name="months[]" value="11">November
	<input type="checkbox" name="months[]" value="12">December

How can I get “checked=“checked”” as another attribute for the form fields whos month numbers appear in the $monthResult array?

It seems like I’m having a mental block tonight and just can’t figure out how to do it!

Hey,

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?

    <?php
    for($i=1; $i <= 12; $i++) {
        $checked = ($i == $monthResult['month']) ? ' checked="checked"' : '';
        $date = date('F', mktime(0, 0, 0, $i, 1, 2011));
        echo '<input type="checkbox" name="months[]" value="'.$i.'"'.$checked.' />'.$date."\
";
    }
    ?>

That makes a timestamp for the first day in 2011 of the given month (defined by the $i variable).

You are correct - I misunderstood what you were trying to do. This should work better:

<?php


$sql2 = "SELECT month FROM seen_month WHERE bird_id = $bird_id";
$result2 = mysql_query($sql2) or die(mysql_error());

$checkedMonths = array();

while($monthResult = mysql_fetch_array($result2, MYSQL_ASSOC)){
    $checkedMonths[] = $monthResult['month'];
}

for($i=1; $i <= 12; $i++) {
    $checked = (in_array($i, $checkedMonths)) ? ' checked="checked"' : '';
    $date = date('F', mktime(0, 0, 0, $i, 1, 2011));
    echo '<input type="checkbox" name="months[]" value="'.$i.'"'.$checked.' />'.$date."\
";
}
?>

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!

Thanks, It does work wonders.

The problem I am trying to solve now is:

<?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.

It is all correct on the database side of things