需要帮助根据codeIgniter中的查询生成动态结果


Need help in generating dynamic result based on the query in codeIgniter

我正试图根据前端传递的查询动态生成结果。这是我的代码。。。。

echo "<table border='1' cellspacing='0' cellpadding='4'><tr>";
foreach ($resultant->list_fields() as $field) {
    echo "<th>$field</th>";
}
foreach ($resultant->result_array() as $row){
    echo "<tr>";
    $resultants = $this->db->query($query);         
    foreach ($resultants->list_fields() as $newFields) {
        echo "<td>$row[$newFields]</td>";
    }
    echo "</tr>";
}
echo "</table>"; 

我得到了预期的结果。我需要继续使用$resultants = $this->db->query($query);

我想知道这会对表现产生影响。所以请指导我。

是的,因为它再次嵌套循环,这将影响性能。据我所知,您需要第二个查询中的所有字段名。以下是避免这种情况的方法。(不过,还有其他方法可以获取字段数)。

echo "<table border='1' cellspacing='0' cellpadding='4'><tr>";
$fieldcount=0;
foreach ($resultant->list_fields() as $field) {
    echo "<th>$field</th>";
$fieldcount++;
}
foreach ($resultant->result_array() as $row){
    echo "<tr>";
    for($i=0; $i<$fieldcount; $i++) {
        echo "<td>$row[$i]</td>";
    }
    echo "</tr>";
}
echo "</table>";