gpt4 book ai didi

php - 将数据(动态结果)拆分为 3 列 - php mysql

转载 作者:行者123 更新时间:2023-11-29 11:45:13 24 4
gpt4 key购买 nike

需要将结果(动态 - mysql)拆分为列(按照下面的 $cols 定义)

注意:如果有人有任何其他格式的解决方案,请告诉我......

$cols=3; // note i can change this to 4,5 ..2 etc..

echo "<table border='0' align='center' valign='top' >";
do{
echo "<tr>";
for($i=1;$i<=$cols;$i++){
$rowsA=data_fetch($sqlquery);
if($rowsA){
echo'<td align="center" valign="top">
<table border="1px" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
<tr align="center" valign="top">';
echo '<td class="dateis" align="center" valign="top" width="35px"> <strong>'.$srno.'</strong> </td>';
echo '<td class="dateis" align="center" valign="top" width="100px">'.$rowsA['prdname'].'</td>';
echo '<td class="dateis" align="center" valign="top" width="50px">'.number_format($rowsA['prdwgh'],3,'.','').'</td>
</tr>';
$srno++;
echo'</tr>
</table>
</td>';
} else{
}
}
} while($rowsA);
echo "</table>";

因此从上面我得到下面的显示:

1 Prda 1.4  |  2 prdan  2.3  |  3  prdbc  3.1
4 prdf 0.02 | 5 prdcv 1.0 | 6 prdd 0.9
7 prdc 0.3 | 8 prdbn 2.8 | 9 prdf 0.5

但我需要如下:

1 Prda 1.4     |  4 prdf   0.02    |  7  prdc   0.3 
2 prdan 2.3 | 5 prdcv 1.0 | 8 prdbn 2.8
3 prdbc 3.1 | 6 prdd 0.9 | 9 prdf 0.5

更新:

如果有 10 条记录:

1 Prda 1.4     |  5 prdcv  1.0    |  9  prdf   0.5
2 prdan 2.3 | 6 prdd 0.9 | 10 prdf 2.5
3 prdbc 3.1 | 7 prdc 0.3 |
4 prdf 0.02 | 8 prdbn 2.8 |

如果有 11 条记录:

1 Prda 1.4     |  5 prdcv  1.0    |  9  prdf   0.5
2 prdan 2.3 | 6 prdd 0.9 | 10 prdf 2.5
3 prdbc 3.1 | 7 prdc 0.3 | 11 prdfd 2.1
4 prdf 0.02 | 8 prdbn 2.8 |

谢谢

最佳答案

cols = 3 的示例。请调整您的数据库名称及其列。希望结果如您所料。

$NoOfColumns = 3;
$dataArr = $matrixArr = array();

$query = "SELECT * FROM testDb ORDER BY prdname";
$result = mysqli_query($conn, $query) or die("error getting data");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
array_push($dataArr, $row);
}
$totalCnt = count($dataArr);
$NoOfRows = floor($totalCnt / $NoOfColumns);
if ($havRem = $totalCnt % $NoOfColumns) {
$NoOfRows++;
}
$matrixArr = array_chunk($dataArr , $NoOfRows);

//echo "<pre>";print_r($dataArr);echo "</pre>";
//echo "<pre>";print_r($matrixArr);echo "</pre>";

echo "<table border=1>";
for ($row = 0; $row < $NoOfRows; $row++) {
echo "<tr>";
$srno = $row+1;
for ($col = 0; $col < $NoOfColumns; $col++) {
echo'<td align="center" valign="top">';
if ($srno<=$totalCnt) {
echo '<table border="1px" cellspacing="0" cellpadding="0" style="border-collapse:collapse;">
<tr align="center" valign="top">';
echo '<td class="dateis" align="center" valign="top" width="35px"> <strong>'.$srno.'</strong> </td>';
echo '<td class="dateis" align="center" valign="top" width="100px">'.$matrixArr[$col][$row]['prdname'].'</td>';
echo '<td class="dateis" align="center" valign="top" width="50px">'.$matrixArr[$col][$row]['prdwgh'].'</td>
</tr>';

echo'</tr>
</table>';
}
echo '</td>';
$srno = $srno+$NoOfRows;
}
echo "</tr>";
}
echo "</table>";

关于php - 将数据(动态结果)拆分为 3 列 - php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35054002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com