2014-11-22 58 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>'; 
?> 
相关问题