简体   繁体   English

在基于codeIgniter的查询中生成动态结果时需要帮助

[英]Need help in generating dynamic result based on the query in codeIgniter

I'm trying to generate the results dynamically based on the query passed in the front end. 我正在尝试根据前端传递的查询动态生成结果。 Here is my code.... 这是我的代码。

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>"; 

I got the expected result. 我得到了预期的结果。 I need to keep on initiating the column values using $resultants = $this->db->query($query); 我需要继续使用$resultants = $this->db->query($query);来初始化列值$resultants = $this->db->query($query);

I wonder this will impact on the performance. 我不知道这会影响性能。 So please guide me on this. 所以请指导我。

Yes as it nested loop again it will impact the performance. 是的,因为它再次嵌套循环会影响性能。 As I understand, you want all the field names in second query. 据我了解,您希望所有字段名称都在第二个查询中。 Here is how you can avoid that. 这是避免这种情况的方法。 (There are other methods to get the number of fields though). (尽管有其他方法可以获取字段数)。

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>"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM