如何使用Codeigniter在PHP中创建具有2个或更多条件的foreach?

时间:2015-03-19 08:53:02

标签: php mysql codeigniter foreach

您好如何在php中创建一个2 foreach循环?我想用Codeigniter格式生成查询。但我在这里遇到了foreach循环的问题,这是查询的代码。

$query = $this -> db -> query("SELECT * FROM CUSTOMERS where CUSTOMER_ID IN (SELECT Customer_ID from Orders)");
$resultOrder = $this -> db -> query ("SELECT * FROM orders");

我想输出这个

foreach (array_merge($resultOrder->result(),$query->result()) as $row  )
{
    echo "<tr>";
    echo "<td>" . $row -> Order_ID . "</td>";
    echo "<td>" . $row -> First_Name . " " . $row ->Last_Name .  "</td>";
    echo "<td>" . $row-> Order_Date . "</td>";
    echo "<td>" . $row->Delivery_Date . "</td>";
    echo "<td>" . $row -> Status . "</td>";
    echo "<td>" . $row ->Remarks . "</td>";
    echo "</tr>";
}

但它有错误。它说

遇到PHP错误

  

严重程度:通知
  消息:未定义属性:stdClass :: $ First_Name
  文件名:xxxx / xxxxxx.php
  行号:xx

谢谢!

编辑:

显然它有输出但$row -> First_Name . " " . $row ->Last_Name的输出与其他输出分开

1 个答案:

答案 0 :(得分:0)

尝试像这样的SQL连接

SELECT * FROM CUSTOMERS c
left join orders o on o.customer_id = c.customer_id
where CUSTOMER_ID IN (SELECT Customer_ID from Orders)