首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据数组打印可变数量的复选框

根据数组打印可变数量的复选框
EN

Stack Overflow用户
提问于 2018-05-28 09:08:01
回答 1查看 43关注 0票数 0

我将介绍一个图像的问题:

这些是jquery中的checkboxradio,我要做的是打印这些复选框,但不是总是以相同的方式打印,因为复选框中的内容每次都会有所不同(因为取决于用户以前在数组中输入的内容)。

例如,建议1在图像上的长度为3,但另一次可能有不同的长度,最长可达10。以及总行数,可以是1或2或…最多10次。

我试着编写一些代码,但它不起作用,主要是因为它有点恶心,而且可能是错误的做法。

代码语言:javascript
复制
<?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...

我知道这对老兵来说很可怕,但我找不到别的东西了。

那么,是否有一种正确打印各种复选框的方法?

答案

多亏了波尔的回答,我成功地做了一些有用的事情,它仍然有点恶心,但它比我原来的方法要好得多:

代码语言:javascript
复制
<?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
}
?>

仍然渴望其他的答案,它可能会做得更好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 11:24:30

我不确定我是否跟随,但如果你有一个变量,名为‘字’,并取决于多少个单词,长度将是不同的。你可以用一个预见来解决这个问题。

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50562754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档