php - 在多列中显示单选框列表

标签 php html css smarty

我有一个 php 循环,它列出了 50 个单选框。 我想在每列上放置 10 个单选框。我试过:

div style="overflow:hidden; white-space:nowrap; float:left;width:160px;">

生成代码:

 {php} $j=0; {/php} 
                {foreach from=$genreLists item=genreList key = genre_key}
                {if $genre_key <= 250}
                div id="genrecheck_{$genreList}">
              input name="genre[]" type="checkbox" id="genre[]" value="{$genreList}" {section  name = rows loop = $smarty.request.genre} {if $genreList ==  $smarty.request.genre[rows]} checked {/if}{/section} onClick="getval(this,'genrecheck_{$genreList}');">
                div id="genre_{$genre_key}" style="display:none;">{$genreList}/div>div id="genre1_{$genre_key}" style="display:inline;">{$genreList} /div>
                /div>
                {php} $j++; if($j%5==0) { echo "
                "; $j=0; } {/php} {/if}{/foreach}

好像不行。有什么建议么?请注意:使用 php 循环回显单选框。

最佳答案

我发现处理将复选框拆分为列的最佳方法是使用 array_chunk() 函数将数组拆分为相等的子数组,然后使用双 foreach 循环来呈现列。

http://php.net/manual/en/function.array-chunk.php

echo '<div class="container">';
foreach(array_chunk($genreLists,5) as $row_value)
{
  echo '<div class="row">';
  foreach($row_value as $cell_key => $cell_value)
  {
    echo '<div class="cell">';
    //echo your checkbox html here
    echo '</div>'; // close cell
  }
  echo '</div>'; // close row
}
echo '</div>'; // close container

如果您在将 array_chunk 分配给模板引擎之前执行它,则可以使用 smarty {foreach} 循环完成相同的概念。实际上在 smarty 中 {$var|array_chunk:5} 应该作为修饰符使用

如果你想让复选框垂直显示,请改用这个函数:

/* ----------[ func ARRAY CHUNK VERTICAL ]----------
A sister to array_chunk, but instead of horizontal, split
the data vertical
*/
function array_chunk_vertical($array = null,$cols = 3, $pad = array(null))
{
  if (is_array($array) == true and !empty($array))
  {
    // total items in the array
    $count = count($array);
    // if count is empty
    if(empty($count))
    {
      return false;
    }
    // if cols is some how still empty
    if(empty($cols))
    {
      $cols = 3;
    }
    // count the number of vertical rows
    $rows = ceil($count/$cols);
    // group the array into colums
    $array = array_chunk($array,$rows);
    // if the array is less that the number of cols required
    // pad it to ensure length remains constant
    if (count($array) < $cols)
    {
      $array = array_pad($array,$cols,$pad);
    }
    // pad the array with a null character as required
    foreach($array as $key => $value)
    {
      $array[$key] = array_pad($value,$rows,null);
    }
    // now inverse the rows with the cols
    foreach($array as $key => $value)
    {
      foreach($value as $sub_key => $sub_value)
      {
        $output[$sub_key][$key] = $sub_value;
      }
    }
    // spit it out
    return $output;
  }
  // oops
  return $array;
}

关于php - 在多列中显示单选框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734887/

相关文章:

php - 如何压缩文件夹并使用php下载它?

php - 如何从公共(public)布局文件中的数据库获取和显示数据并显示在 laravel 5.2 中的所有 View 上

html - Firefox 中的列规则属性会产生错误的颜色结果

html - 如何使 Twitter Bootstrap 按钮变大?

html - 使用 Font Faces - 您自己的字体

jquery - 使用 jQuery,在悬停在不同元素上时更改 SVG 颜色

css - div没有100%展开,还请反馈

php - 从 mysql 查询中选择不同的 sumof

共享 Linux 主机上的 PHP 和 MS Access 2007

html - 防止背景在 .dropdown-toggle 组件上改变颜色