display a list of radio boxes in multiple columns

此生再无相见时 提交于 2019-12-02 16:18:07

问题


I have a php loop that lists 50 radio boxes. I want to put 10 radio boxes on each column. I tried:

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

generating code:

 {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}

it seems like its not working. Any suggestions? please note: the radio boxes are echoed using a php loop.


回答1:


I've found the best way to deal with splitting check boxes into columns is with the array_chunk() function to split your array into equal sub arrays, and then use a double foreach loop to render the columns.

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

The same concept can be done with the smarty {foreach} loop provided you perform the array_chunk before assigning it to the template engine. Actually in smarty {$var|array_chunk:5} should work as a modifier

If you want to have the check boxes represented vertically, use this function instead:

/* ----------[ 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;
}



回答2:


<div class="Row">
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
  <input type="radio" name="Row1" />
</div>
<div class="Row">
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
  <input type="radio" name="Row2" />
</div>
<div class="Row">
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
  <input type="radio" name="Row3" />
</div>
<div class="Row">
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
  <input type="radio" name="Row4" />
</div>
<div class="Row">
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
  <input type="radio" name="Row5" />
</div>


来源:https://stackoverflow.com/questions/8734887/display-a-list-of-radio-boxes-in-multiple-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!