我将用图片介绍问题:
这些是来自jquery的checkboxradio,我正在尝试做的是 打印这些复选框但不总是相同的方式,因为复选框中的内容每次都不同(因为取决于用户先前在数组中输入的内容)
例如,建议1在图像上的长度为3,但是可以在另一时间具有不同的长度,长度为10。 行总数,可以是1或2或......最多10个。
我尝试编写代码,但它不起作用,主要是因为它有点恶心,可能是一种错误的做法。
<?php
/*
$words is a 2 dimensional array, which contains :
row -> specific type (ex : Metal's type / Water's name...)
column -> each words of a type (ex : Iron / Copper / Barium...)
*/
$i = 1; //Iterator through lignes
$j = 0; //Iterator through columns
$length = 0; //Length of the array
/*
Print a certain number of radio button type
*/
if(!empty($words['0']) && $i==1) {
$length = count($words['0']); //Needed to know how many radio button we have to show
debug($length);
echo('Mot : 1');
//Now things starts to be wrong
?>
<html>
<fieldset>
<legend>Choose one : </legend>
<!--
Print radio button depending on the number of words,
could be all of them, or only 1.
-->
</html>
<?php
if($j < $length) {
?>
<html>
<label for="radio-1">$words['0']['0']</label>
<input type="radio" name="radio-1" id="radio-1">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-2">$words['0']['1']</label>
<input type="radio" name="radio-1" id="radio-2">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-3">$words['0']['2']</label>
<input type="radio" name="radio-1" id="radio-3">
</html>
<?php
$j++;
}
if($j < $length) {
?>
<html>
<label for="radio-4">$words['0']['3']</label>
<input type="radio" name="radio-1" id="radio-4">
</html>
<?php
$j++;
}
?>
<!--... Up to 10 times-->
</fieldset>
</html>
<?php
}
$i++; //Moving to the next row
//Same as above, but on an other row
if(!empty($words['1']) && $i==2) {
$length = count($words['0']);
echo('Mot : 2');
//etc...
我知道这对退伍军人来说是可怕的,但我找不到别的东西。
那么,有没有办法正确打印各种复选框?
ANSWER
感谢Pol的回答,我设法做了一些有用的事情,它仍然有点恶心,但它比我原来的方式更好:
<?php
$i=1;
foreach ($newkeywords as $words) {?>
<h2>Groupe <?php $i?></h2>
<fieldset>
<legend>word '<?php echo($words[0])?>' :</legend>
<?php foreach ($words as $word) {?>
<label for=<?php 'checkbox-'.$i ?>><?php echo($word)?></label>
<input type="checkbox" name=<?php 'checkbox'.$i ?> id=<?php
'checkbox'.$i ?></br>
<?php $i=$i+1;?>
<?php
}
$i=1;
?></br><?php
}
?>
仍然渴望得到其他答案,它可能会做得更好
答案 0 :(得分:0)
我不确定我是否已关注,但如果你有一个名为&#39; words&#39;并且取决于长度不同的单词数量。你可以用foreach来解决这个问题。
foreach($words as $word) :
// here you just print the row
// because the foreach will go through all words one by one.
// if you dont know how to access $word you can use var_dump($word)
// and then figure it out
echo '<label for="radio-1">'.$word['0'].'</label>';
echo '<input type="radio" name="radio-1" id="radio-1">':
endforeach;