php - 在 codeigniter 中使用 PHP 导出表中的对象数组

标签 php mysql arrays codeigniter

我有一个看起来像这样的对象数组:

Done:  array(2) {
[0]=>
object(stdClass)#107 (19) {
["id"]=>
string(1) "6"
["apartament"]=>
string(1) "1"
["nume"]=>
string(5) "SURCA"
["persoane"]=>
string(1) "2"
["mp"]=>
string(4) "37.7"
["retim"]=>
string(5) "19.19"
["incalzire"]=>
string(5) "74.74"
["apacaldamc"]=>
string(1) "3"
["apacaldalei"]=>
string(5) "47.48"
["apacaldadif"]=>
string(4) "3.72"
["aparecemc"]=>
string(1) "8"
["aparecelei"]=>
string(5) "54.73"
["aparecedif"]=>
NULL
["curent"]=>
string(3) "345"
["gaz"]=>
string(3) "2.5"
["administrator"]=>
string(5) "17.01"
["cheltuieliadministrare"]=>
string(4) "2.05"
["acoperis"]=>
string(4) "62.5"
["timp"]=>
string(19) "2017-04-28 10:04:28"
}
[1]=>
object(stdClass)#108 (19) {
["id"]=>
string(1) "7"
["apartament"]=>
string(1) "2"
["nume"]=>
string(8) "ENACHIUC"
["persoane"]=>
string(1) "1"
["mp"]=>
string(5) "37.07"
["retim"]=>
string(3) "9.6"
["incalzire"]=>
string(4) "73.5"
["apacaldamc"]=>
string(1) "3"
["apacaldalei"]=>
string(5) "15.83"
["apacaldadif"]=>
string(4) "3.72"
["aparecemc"]=>
string(1) "2"
["aparecelei"]=>
string(5) "13.68"
["aparecedif"]=>
string(1) "0"
["curent"]=>
string(4) "0.66"
["gaz"]=>
string(4) "1.25"
["administrator"]=>
string(5) "17.01"
["cheltuieliadministrare"]=>
string(4) "2.05"
["acoperis"]=>
string(4) "62.5"
["timp"]=>
string(19) "2017-04-28 10:11:25"
}
}

我从标准查询中得到这个:

$sql ="SELECT * FROM consum WHERE timp BETWEEN '".$fromDate."' AND '".$toDate."'";
$query = $this->db->query($sql);
$databaseOject = $query->result();

我试图做这样的事情:

if ($databaseOject->num_rows > 0) {
        echo "<table><tr><th>id</th><th>apartament</th><th>nume</th><th>apartament</th><th>persoane</th><th>mp</th><th>retim</th><th>incalzire</th><th>apacaldamc</th><th>apacaldalei</th><th>apacaldadif</th><th>aparecemc</th><th>aparecelei</th><th>aparecedif</th><th>curent</th><th>gaz</th><th>administrator</th><th>cheltuieliadministrare</th><th>acoperis</th><th>timp</th></tr>";
// output data of each row
        while($row = $databaseOject->fetch_assoc()) {
            echo "<tr><td>".$row["id"]."</td><td>".$row["apartament"]."</td><td>".$row["nume"]."</td><td>".$row["persoane"]."</td><td>".$row["mp"]."</td><td>".$row["retim"]."</td><td>".$row["incalzire"]."</td><td>".$row["apacaldamc"]."</td><td>".$row["apacaldalei"]."</td><td>".$row["apacaldadif"]."</td><td>".$row["aparecemc"]."</td><td>".$row["aparecelei"]."</td><td>".$row["aparecedif"]."</td><td>".$row["curent"]."</td><td>".$row["gaz"]."</td><td>".$row["administrator"]."</td><td>".$row["cheltuieliadministrare"]."</td><td>".$row["acoperis"]."</td><td>".$row["timp"]."</td></tr>";
    }
        echo "</table>";
    } else {
        echo "0 results";
}

但我得到:

Trying to get property of non-object

确实 $databaseOject 不是一个对象,它是一个对象数组,可能这就是我收到该错误的原因,但是我如何在我的情况下制作表格 - 对象数组?非常感谢您抽出时间!

最佳答案

$query->result() 的结果是一个 StdObject 数组,您可以使用简单的 foreach 循环遍历它们。
要访问它们的属性,因为它们是 stdObjects 而不是数组,您需要将 $row['id'] 替换为 $row->id,或替换 result()result_array()

我强烈建议您阅读有关该主题的文档 here .

关于php - 在 codeigniter 中使用 PHP 导出表中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43686019/

相关文章:

c - typedef 中的动态二维数组

php - 如果当前日期和时间已过,如何更改文本颜色?

php - 将数组内容存入数据库

php - 如何在wordpress函数中跳过具有特定类别的图像

django - mySQL CSV 导入中的未知列

python - 如何从 python 中保存/检索 matlab 执行的内容?

c++ - 数组转移到下一个元素

没有 cron 的 PHP 调度程序

php - 在 DB、半尺寸和全尺寸中搜索尺寸

php - 插入查询后,mysqli_insert_id()在同一连接上返回0