选择仅在一个表中工作的多个表

时间:2014-11-04 16:53:48

标签: php mysql

我正在尝试连接四个表。 order_historyorders_listinfistall_orderdelivery_orders。当我将代码运行到order_history表中时,结果就像我期待的那样。但是,当我将代码运行到网页中时,它只会从order_history表中选择值。

注意:将代码运行到order_history表中我的意思是当我输入phpMyAdmin并转到order_history表并使用我的SQL代码在那里运行SQL。

<?php
include ("config.php");
$results = $mysqli->query
("
    SELECT              orders_history.transaction_id,
                        orders_history.items,
                        orders_history.quantity,
                        orders_history.one_product_price,

                        orders_list.status,
                        orders_list.invoices,
                        orders_list.payment_method,
                        orders_list.order_method,

                        infistall_order.address,

                        delivery_orders.address,
                        delivery_orders.service,
                        delivery_orders.cost,
                        delivery_orders.city

    FROM                orders_history

    LEFT JOIN           orders_list
    ON                  orders_history.transaction_id = orders_list.transaction_id

    LEFT JOIN           infistall_order
    ON                  orders_history.transaction_id = infistall_order.transaction_id

    LEFT JOIN           delivery_orders
    ON                  orders_history.transaction_id = delivery_orders.transaction_id

    WHERE               orders_list.customer_name = 'Klaudia'"      
);

实际上我正在尝试根据transaction_id收集所有表格中所有列的信息。

$orders = array();     
$html = '';     
if ($results) {
    while($obj = $results->fetch_object()) {
        $orders[$obj->transaction_id][$obj->items] = array('quantity' => $obj->quantity, 'invoices' => $obj->one_product_price);
    }

    $html .= '<table width="70%"><tr>';
    $html .= '<td>items</td>';
    $html .= '<td>quantity</td>';
    $html .= '<td>one_product_price</td>';
    $html .= '<td>status</td>';
    $html .= '<td>invoices</td>';
    $html .= '<td>payment_method</td>';
    $html .= '<td>order_method</td>';
    $html .= '<td>address</td>';
    $html .= '<td>service</td>';
    $html .= '<td>cost</td>';
    $html .= '<td>city</td></tr>';
    foreach ($orders AS $order_id => $order) {
        $html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>';
        $row = 1;
        foreach ($order AS $item => $data) {
            if ($row > 1) { $html .= '</tr><tr>'; }
            $html .= '<td>' . $item . '</td>';
            $html .= '<td>' . $data['items'] . '</td>';
            $html .= '<td>' . $data['quantity'] . '</td>';
            $html .= '<td>' . $data['one_product_price'] . '</td>';
            $html .= '<td>' . $data['status'] . '</td>';
            $html .= '<td>' . $data['invoices'] . '</td>';
            $html .= '<td>' . $data['payment_method'] . '</td>';
            $html .= '<td>' . $data['order_method'] . '</td>';
            $html .= '<td>' . $data['address'] . '</td>';
            $html .= '<td>' . $data['service'] . '</td>';
            $html .= '<td>' . $data['cost'] . '</td>';
            $html .= '<td>' . $data['city'] . '</td>';
            $row++;
        }
        $html .= '</tr><tbody>';
    }
    $html .= '</table>';
} 
echo $html;
?>

1 个答案:

答案 0 :(得分:1)

在你的循环中:

    while($obj = $results->fetch_object()) {
        $orders[$obj->transaction_id][$obj->items] = array(
            'quantity' => $obj->quantity, 
            'invoices' => $obj->one_product_price
        );
    }

您可以将其更改为:

    while($obj = $results->fetch_object()) {
        $orders[$obj->transaction_id][$obj->items][] = array(
            'quantity' => $obj->quantity, 
            'invoices' => $obj->one_product_price
        );
    }

它将阻止数据相互替换,数组上的[]符号使数组变为动态,键将从0开始计数,并且当您向其中添加更多数据时将增加1那个阵列。