简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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