![](/img/trans.png)
[英]mongoose/mongodb - Arrange documents in descending order based on a document's index, placing the result in an array
[英]How to go over an array result again in order to arrange (by one of the fields) the result into a table in PHP
我必須按位置,提供者或資產描述的順序進行“詳細報告”,盡管我得到了正確的結果,但我想顯示類似
位置1:
此處的表以及該位置的資產
位置2:
資產位於此位置的表格...
與提供者和描述相同...
我的PHP和其他語言知識有限。 所以,我想到了做類似...
獲取數組(完整的數組,不僅是一行),然后檢查array[i]
的位置是否等於array[i+1]
然后輸出行數據打印<td>
,否則結束該表,然后創建一個新
位置:
最后是另一個表,其中的行與該位置相匹配...
如何在PHP中做到這一點?
例子:
這是我目前擁有的
這就是我想做的
這就是我在PHP中所做的
<?php
echo "<table>
<thead>
<tr>
<th></th>
<th>Ubicación</th>
<th>Código</th>
<th>Descripción</th>
<th>Costo Adquisición</th>
<th>Saldo a Depreciar de Activos</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>";
while($row = odbc_fetch_array($result)){
echo"<td></td>
<td><b>".$row["Ubicacion_actual"]."</b></td>
<td>".$row["Activo"]."</td>
<td>".$row["Descripcion"]."</td>
<td>".number_format($row["Costo_Adquisicion"],4)."</td>
<td>".number_format($row["Saldo_sin_depreciar"],4)."</td>
<td></td>
</tr>";
}
echo "</tbody>
</table>";
odbc_close($dbhandle);
?>
首先,您應該將所有數據放入本地數組,然后使用本地數組進行處理。
因此您可以:
$theArray = array(); //Array to hold data
$headerIds = array(); //Array to hold unique id of the records
while($row = odbc_fetch_array($result)){
array_push($theArray, $row);
if(!in_array($row["Ubicacion_actual"], $headerIds))
{
array_push($headerIds, $row["Ubicacion_actual"]);
}
}
odbc_close($dbhandle);
foreach($headerIds as $id)
{
//Print the header info
//...
//Loop through each item in your data array until its id matches the header id
foreach($theArray as $dataItem)
{
if($dataItem["Ubicacion_actual"] == $id) //This line item has the same id as the header
{
//Display the table row data
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.