根据阵列数填充复选框 [英] Populate checkboxes according to array count
问题描述
进出口新的 PHP
及其发展我已经使用 PHP数组
。我想根据阵列数来填充复选框
。在奥得这样做,我已经尝试以下方法。它没有对我的工作。是否有办法做到这一点(在我的情况下,阵列数= 5,所以我需要5相应的复选框)
< PHP $ chk_group =阵列('1'=>'红',
'2'= GT; AA,
'3'= GT; '日',
'4'= GT; '岭',
'5'= GT; 莎拉 ); 后续代码var_dump($ chk_group); //继续循环 为($ I = 0; $ I<计数($ chk_group); $ I ++)
{
//回声计数($ chk_group);
回声<输入类型=复选框值=$ chk_groupNAME =chk_group [0]>中
回声$ chk_group;
}
?>
您是通过不逃避报价pmaturely结束你的回音字符串$ P $。看到这里的问题:
//见回声在字符串为输入属性的开头如何结束
//标记,另一个字符串开始在结束了吗?需要逃避的报价。
回声<输入类型=复选框VALUE =SOME_VALUENAME =some_name>中;//事情是这样的 - 请注意字符串的结尾,它应该。
回声<输入类型= \\复选框\\价值= \\SOME_VALUE \\NAME = \\some_name \\>中;
你有另外一个问题是您使用< PHP ..>
PHP标记内
此外,要与呼应阵列中的键关联的值。你在这里什么是关联数组(键=>值对),而不是一个更基本的数组(索引值)。
最后,你应该理想地利用关联数组一个的foreach
循环。下面是读数我建议你做的。
请参阅: http://php.net/manual/en/language。 types.array.php
请参阅: http://php.net/manual/en/control- structures.foreach.php
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;
}
?>
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
这篇关于根据阵列数填充复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!