0

模型

    $id = $this->session->userdata('id');

    $q = $this->db->get_where('shipping_address', array('customer_id' => $id));

    if ($q->num_rows == 0)
    {
        $data['id'] = 'You dont have a shipping address saved.';

    } else {
        foreach ($q->result() as $row) 
            {
            $data['first'] = $row->firstname;
            $data['last'] = $row->lastname;
            }
    }
    return $data;

控制器

    $this->load->model('Customer_accounts');

        $customer = $this->Customer_accounts->get_customer_info();
        $ship = $this->Customer_accounts->shipping_address();

        $data = $ship + $customer;

    $this->load->view('account_dashboard/personal_information', $data);

看法

<?php foreach ($ship as $row) : ?>
    <table class="fixCap" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><?php echo $row['firstname'] . $row['lastname']; ?></td> 
      </tr>

           . . .

      </tr>
    </table>
<?php endforeach; ?>    

var_dump

只显示array表的 1rows但它应该显示包含定义的 2 行customer_id

问题

无法将所有数据库传递dataforeach我做错了什么?

4

3 回答 3

1

利用:

$data = array_merge($ship, $customer); // they will merged as one array

或者如果你想让它们分开你可以这样做

$data = array();
$data['ship'] = $ship;
$data['customer'] = $customer;

//In the view
//ship
foreach($data['ship'] as $ship)
{
   //Ship values
}

//customer
foreach($data['customer'] as $customer)
{
   //Customer value
}

谢谢

于 2012-10-11T21:48:25.503 回答
0

您实际上并没有使用您的代码从数据库中提取结果。

尝试这样的事情

$ret =  $q->get()->result();

foreach($ret as $row) {

}

于 2012-10-11T21:48:27.843 回答
0

问题出在我Model将上面的代码替换为

模型

return $this->db->get_where('shipping_address', array('customer_id' => $id))->result();

而在我的

看法

#I echoed the vars as objects 
$row->firstname
于 2012-10-11T21:55:00.870 回答