簡體   English   中英

在基於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);來初始化列值$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>"; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM