2012-01-04 3 views
0

50 개의 라디오 상자를 나열하는 PHP 루프가 있습니다. 각 열에 10 개의 라디오 박스를 넣고 싶습니다. 나는 시도 :여러 열에 라디오 상자 목록 표시

DIV 스타일 = "오버 플로우 : 숨겨진; 공백 : 파라미터 nowrap; 플로트 : 왼쪽; 폭 : 160 픽셀;">

생성 코드 :

{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 루프를 사용하여 에코됩니다.

+2

이 좀 더 구체적으로 수를 생성 코드를 제공 하시겠습니까? – Rodaine

+0

나는 코드를 추가했다. 코드는 멋지지만 그 형태는 PHP와 같다. left'와'표시 : 각 유식 정말 잘 알고 루프 – cppit

+1

아니지만, 플로트'와 사업부의 각 (10 개)의 체크 박스를 포장, 카운터를 추가 block'을 체크 박스에 트릭을 할해야 - 당신이 나를 보여줄 수 있다면 나는 – ptriek

답변

1

배열을 균등 한 하위 배열로 분할 한 다음 열을 렌더링하는 데 double foreach 루프를 사용하는 array_chunk() 함수를 사용하여 열을 나누는 체크 상자를 처리하는 가장 좋은 방법을 찾았습니다.

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 

동일한 개념

이 유식 수행 할 수 있습니다 {foreach는} 루프는 템플릿 엔진에 할당하기 전에 array_chunk을 수행 제공. 실제로 유식에서 {$ 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; 
} 
1
<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> 
+0

라디오는 PHP를 사용하여로드됩니다 :)하지만 올바른 개념을 제임스는 제임스 – cppit