【问题标题】:How to go over an array result again in order to arrange (by one of the fields) the result into a table in PHP如何再次检查数组结果以便(通过其中一个字段)将结果排列到 PHP 中的表中
【发布时间】:2017-11-03 05:32:29
【问题描述】:

我必须按位置、提供商或资产描述排序“详细报告” 问题是,虽然我得到了正确的结果,但我想显示类似的结果

位置 1:

此处的表格包含位于该位置的资产

位置 2:

具有位于此位置的资产的表...

与提供者和描述相同...

我的 PHP 和其他语言知识有限。所以,我想做一些类似的事情......

获取数组(整个数组不仅仅是一行),然后检查array[i] 中的位置是否等于array[i+1],然后打印带有行数据的<td>,否则结束该表,然后创建一个新的

地点:

最后是另一个包含与该位置匹配的行的表...

我如何在 PHP 中做到这一点?

例子:

This我目前拥有的

This 是我想做的事

这就是我在 PHP 中所做的事情

<?php

echo "<table>
        <thead>
            <tr>
                <th></th>
                <th>Ubicaci&oacute;n</th>
                <th>C&oacute;digo</th>
                <th>Descripci&oacute;n</th>
                <th>Costo Adquisici&oacute;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);
?>

【问题讨论】:

    标签: javascript php jquery html-table resultset


    【解决方案1】:

    首先你应该把所有的数据放到一个本地数组中,然后使用本地数组来处理它。

    所以你可以:

    $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
            }
        }
    }
    

    【讨论】:

    • 您好,唯一的问题是表没有 ID
    • 不是每一行都有一个“Ubicacion_actual”吗?我以为您是按此对结果进行分组的。当我有 $id 时,我将它用作对数据进行分组的变量。所以任何具有相同“Ubicacion_actual”的东西都将在同一个组中。
    • 做到了,得到victorianoguera.icad.com.ve/result.html 并在$theArray 和$HeaderIds victorianoguera.icad.com.ve/result_var.html 上使用var_dump ...我在这里遇到错误:if(!in_array($row["Ubicacion_actual"]) , $headerIds) 所以我把它改成这样: if (!in_array($row["Ubicacion_actual"], $headerIds));完整代码在这里:victorianoguera.icad.com.ve/code.txt
    • 哦,是的,我在 inarray 调用中犯了一个错误。那么它现在工作了吗?
    • 是的!我修复了它,我得到它的错误是由于;如果那没有去那里....谢谢!
    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多