2012-04-05 31 views
0

我有一個(1),2,3,4,5或6個結果的(php)數組。我想顯示它們作爲下列中的不同情況:在2列中的訂單結果

1結果:

[result 1] 

2結果:

[result 1] [result 2] 

3分的結果:

[result 1] [result 3] 
[result 2] 

4的結果:

[result 1] [result 3] 
[result 2] [result 4] 

5結果:

[result 1] [result 4] 
[result 2] [result 5] 
[result 3] 

6分的結果:

[result 1] [result 4] 
[result 2] [result 5] 
[result 3] [result 6] 

如果我能這樣用CSS只(而不是與課程的表),所以正確的順序是保留這將是很好來源,但顯示如上。否則,我想我需要一些奇怪的PHP循環來在屏幕上以正確的順序得到這些結果。任何人都知道如何做到這一點?提前致謝!

+0

看到這個討論:http://stackoverflow.com/questions/8705323/2-row-element-layout-within-horizo​​ntal-div/8705916#8705916 - 也許可以幫助 – fcalderan 2012-04-05 09:06:46

+0

查看http://stackoverflow.com/questions/10022353/how-can-i-style-ul-li-in-css/10022573#10022573 – 2012-04-05 09:10:32

+0

向我們展示您的陣列的樣本 – Starx 2012-04-05 09:33:22

回答

0

它猜測這是不可能的,只能用css做一些事情。您將不得不將數組分成兩部分,並將前半部分顯示到第一列,將後半部分顯示到第二列。不應該很難做到?

0

喜歡的東西

$out = "<table><tbody>"; 
for ($i = 0; $i < count($array); $i++){ 
    $el = $array[$i]; 
    if(($i % 2) === 0){ 
     $out .= '<tr>'; 
    } 
    $out .= "<td>$el</td>"; 
    //Handlethe case that this is the last iteration and 
    //the elements of the array are odd 
    if((($i % 2) === 1) && (($i + 1) === count($array))){ 
     $out .= "<td></td></tr>"; 
    }elseif(($i % 2) === 1){ 
     $out .= "</tr>"; 
    } 
} 

$out .= "</tbody></table>"; 
+0

感謝您的幫助,但沒有表格請:) – 2012-04-05 12:54:56

+0

@ user468893您可以輕鬆地調整代碼以使用div而不是tr和span而不是td – 2012-04-05 13:01:14

0
$array = array(1, 2, 3, 4, 5, 6, 7); 

$number_of_columns = floatval(2.0); // float to make the below ceil work 
$number_of_items = count($array); 
$items_per_column = ceil($number_of_items/$number_of_columns); 
$current_column = 0; 

for ($i = 0; $i < $number_of_items; $i++){ 

    if ($i % items_per_column == 0){ 
     $current_column++; 
    } 

    echo $i, ' -> ', $current_column, '<br />'; 
}