问题
im new to php
and its developing i have used php array
. I want to populate checkboxes
according to array count. in oder to do that i have tried following way. it didnt work on me. Are there any way to do that (in my case array count= 5 so i need 5 checkboxes accordingly)
<?php
$chk_group =array('1' => 'red',
'2' => 'aa',
'3' => 'th',
'4' => 'ra',
'5' => 'sara'
);
var_dump($chk_group);
//continue for loop
for ($i=0 ; $i<count($chk_group);$i++)
{
// echo count($chk_group);
echo"<input type="checkbox" value="$chk_group" name="chk_group[0]">"
echo $chk_group;
}
?>
回答1:
You're ending your echo strings prematurely by not escaping the quotations. See the problem here:
// See how the echo string ends at the beginning of the attributes for the input
// tag, and another string begins at the end? Need to escape the quotations.
echo "<input type="checkbox" value="some_value" name="some_name">";
// Something like this -- notice how the string ends where it should.
echo "<input type=\"checkbox\" value=\"some_value\" name=\"some_name\">";
Another problem you have is your use of <?php .. ?>
within PHP tags.
Additionally, you want to echo the values associated with the keys in your array. What you have here is an associative array (key => value pairs), as opposed to a more rudimentary array (indexed values).
Finally, you should ideally utilize a foreach
loop with associative arrays. Below are readings I recommend you do.
See: http://php.net/manual/en/language.types.array.php
See: http://php.net/manual/en/control-structures.foreach.php
来源:https://stackoverflow.com/questions/27585903/populate-checkboxes-according-to-array-count