【问题标题】:Populate checkboxes according to array count根据数组计数填充复选框
【发布时间】:2015-02-19 13:28:33
【问题描述】:

我是php 的新手,我使用过php array。我想根据数组计数填充checkboxes。为了做到这一点,我尝试了以下方式。它对我不起作用。有没有办法做到这一点(在我的情况下数组计数= 5,所以我需要相应的 5 个复选框)

 <?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;  
     }


         ?>

【问题讨论】:

    标签: php arrays checkbox


    【解决方案1】:

    通过不转义引号,您过早地结束了回显字符串。在这里查看问题:

    // 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\">";
    

    您遇到的另一个问题是您在 PHP 标记中使用了&lt;?php .. ?&gt;

    此外,您希望回显与数组中的键关联的值。您在这里拥有的是关联数组(键 => 值对),而不是更基本的数组(索引值)。

    最后,您最好使用带有关联数组的foreach 循环。以下是我建议您阅读的内容。

    见:http://php.net/manual/en/language.types.array.php

    见:http://php.net/manual/en/control-structures.foreach.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 2020-10-01
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多