2014-11-22 59 views
1

我想要的是在1個表中顯示檢索數據。但我需要的錶行10是極限,然後轉移到另一列在一個表中顯示從數據庫垂直檢索的數據

輸出示例:

data1 data11 
data2 data12 
data3 data13 
data4 data14 
data5 data15 
data6 data16 
data7 data17 
data8 data18 
data9 data19 
data10 data20 
+0

你需要在表中做這個嗎?使用div和css會更容易。 – Sean 2014-11-22 03:37:06

+0

我認爲你可以使用'ul'和'li'來實現,將固定高度設置爲ul並將固定寬度設置爲li。 – 2014-11-22 03:37:44

+0

你能舉個例子嗎? – rapidoodle 2014-11-22 03:39:26

回答

1

而不是使用一個表,這是一個更具有挑戰性得到你想要的格式,我會建議使用div■如果您想使用的表,你可以風格像一個表

<style> 
    // create a column class with a width and float to the left 
    .column {width:100px;float:left;} 
</style>"; 

<?php 
// open/create 1st column 
echo "<div class='column'>\n"; 

// create a range for example 
$range = range(1,20); 

foreach($range as $r){ 

    // after 10 records, close the last column and open/create a new column 
    if($r!=1 && $r%10==1){echo "</div>\n<div class='column'>\n";} 

    // echo your data in a 'cell' 
    echo "<div class='cell'>data{$r}</div>\n"; 
} 

// close last column 
echo "</div>"; 
?> 
+0

即時通訊使用mpdf如此使用浮動:左是有限的 – rapidoodle 2014-11-22 04:08:42

+0

它可能是有限的,但根據[文檔](http://mpdf1.com/manual/index .php?tid = 385),即使只是「部分」支持它,也不知道爲什麼它會成爲問題。 – Sean 2014-11-22 04:15:42

1

您可以使用可變的變量數據數組很容易,我認爲是這樣的:

$i=0; 
$j=0; 
while($result=fetch_result()) 
{ 
    $colVar=''; 
    for($depth=0;$depth<=$j;$depth++) 
    { 
     $colVar.='['.$i.']'; 
    } 
    $outputArray{$colVar}=$result; 
    $i++; 
    if($i>9) 
    { 
     $j++; 
     $i=0; 
    } 
} 

這將爲前10行創建索引爲0-9的數組,然後爲每10行添加一個維度。

如果在結果THIRY一個行的數據是這樣的:

$outputArray[0]=data1; 
$outputArray[0][0]=data11; 
$outputArray[0][0][0]=data21; 
$outputArray[0][0][0][0]=data31; 
$outputArray[1]=data2; 
$outputArray[1][1]=data12; 
$outputArray[1][1][1]=data22; 
... 
... 
$outputArray[9]=data10; 
$outputArray[9][9]=data20; 
$outputArray[9][9][9]=data30; 

然後,您可以整齊地從基於陣列的深度數據創建一個表 - 這意味着,如果它是一個單個數組,製作一列,如果它是兩個深度,製作兩列等。

2

<?php 

//assuming $data is an array which already contains your data 
$data = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); 

$rowsPerColumn = 10; 

$columns = ceil(count($data)/$rowsPerColumn); 

echo '<table>'; 

for ($r = 0; $r < $rowsPerColumn; $r++) 
{ 
    echo '<tr>'; 
    for ($c = 0; $c < $columns; $c++) 
    { 
     $cell = ($c * $rowsPerColumn) + $r; 
     echo '<td>' . (isset($data[$cell]) ? $data[$cell] : '&nbsp;') . '</td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 
?> 
相關問題