显示命令的PHP购物车

时间:2012-04-21 23:59:48

标签: php mysql multidimensional-array pdo shop

HY,

我正在为培训目的创建一个PHP商店。我希望用户可以查看他放置的最后一个订单 - 但我有意识到这一点。

我在我的数据库中有这些表:

客户(孩子,姓名,地址,......)
产品(pid,product_name,product_description,price)
订单 - > oid,孩子,付款,地址,状态
order_detail - > oid,date,pid,quantity)

好的,我已经创建了一个函数,其中查询从数据库中获取所需的数据

function showOrder($kid)
{
$db = database();
$orders = $db->query ("SELECT * FROM orders
    INNER JOIN order_detail on orders.oid=order_detail.oid
    INNER JOIN products on order_detail.pid = products.pid
    WHERE kid='$kid' ");

$orders = $orders->fetchAll();

$lastoid = 0;
foreach($orders as $i){

    while($lastoid != $i['oid']) {
        $lastoid = $i['oid'];
        echo "Ordernr: ".$lastoid."<br/>";
        echo "Produktname: ".$i['product_name']."<br>";
        echo "Menge: ".$i['quantity']."<br/>";
        echo "Preis: ".$i['price']."<br/>";
        echo "Status: ".$i['status']."<br/>";
        echo "<br/><br/><hr/>";    
    }
}
}

我想做什么: 列出表中的单个订单( - &gt; oderid |产品名称|数量|价格|状态) 如果订单只包含一个产品,但如果订单变大(2个产品),则仅显示第一个产品。

$ database看起来像这样:

Array
(
[0] => Array
    (
        [oid] => 1
        [0] => 1
        [kid] => 1
        [1] => 1
        [2] => 1
        [date] => 2012-04-17
        [3] => 2012-04-17
        [pid] => 1
        [4] => 1
        [quantity] => 2
        [5] => 2
        [payment] => Nachnahme
        [6] => Nachnahme
        [street] => teststraße
        [7] => teststraße
        [number] => 2
        [8] => 2
        [zip] => 2222
        [9] => 2222
        [city] => Teststadt
        [10] => Teststadt
        [status] => in Bearbeitung
        [11] => in Bearbeitung
        [12] => 1
        [product_name] => Acer Laptop
        [13] => Acer Laptop
        [price] => 29.00
        [14] => 29.00
        [details] => blabla
        [15] => blabla
        [category] => Laptop
        [16] => Laptop
        [date_added] => 2012-04-05
        [17] => 2012-04-05
    )

[1] => Array
    (
        [oid] => 1
        [0] => 1
        [kid] => 1
        [1] => 1
        [2] => 1
        [date] => 2012-04-17
        [3] => 2012-04-17
        [pid] => 2
        [4] => 2
        [quantity] => 2
        [5] => 2
        [payment] => Nachnahme
        [6] => Nachnahme
        [street] => teststraße
        [7] => teststraße
        [number] => 2
        [8] => 2
        [zip] => 2222
        [9] => 2222
        [city] => Teststadt
        [10] => Teststadt
        [status] => in Bearbeitung
        [11] => in Bearbeitung
        [12] => 2
        [product_name] => Grundig TV
        [13] => Grundig TV
        [price] => 22.00
        [14] => 22.00
        [details] => blabla

        [15] => blabla
        [category] => TV
        [16] => TV
        [date_added] => 2012-04-05
        [17] => 2012-04-05
    )

[2] => Array
    (
        [oid] => 1
        [0] => 1
        [kid] => 1
        [1] => 1
        [2] => 1
        [date] => 2012-04-17
        [3] => 2012-04-17
        [pid] => 7
        [4] => 7
        [quantity] => 1
        [5] => 1
        [payment] => Nachnahme
        [6] => Nachnahme
        [street] => teststraße
        [7] => teststraße
        [number] => 2
        [8] => 2
        [zip] => 2222
        [9] => 2222
        [city] => Teststadt
        [10] => Teststadt
        [status] => in Bearbeitung
        [11] => in Bearbeitung
        [12] => 7
        [product_name] => Nokia Handy
        [13] => Nokia Handy
        [price] => 69.00
        [14] => 69.00
        [details] => blabla
        [15] => blabla
        [category] => Handy
        [16] => Handy
        [date_added] => 2012-04-06
        [17] => 2012-04-06
    )

[3] => Array
    (
        [oid] => 2
        [0] => 2
        [kid] => 1
        [1] => 1
        [2] => 2
        [date] => 2012-04-17
        [3] => 2012-04-17
        [pid] => 8
        [4] => 8
        [quantity] => 1
        [5] => 1
        [payment] => Vorauskasse
        [6] => Vorauskasse
        [street] => musterstraße
        [7] => musterstraße
        [number] => 1
        [8] => 1
        [zip] => 1111
        [9] => 1111
        [city] => stadt
        [10] => stadt
        [status] => in Bearbeitung
        [11] => in Bearbeitung
        [12] => 8
        [product_name] => PC groß
        [13] => PC groß
        [price] => 66.00
        [14] => 66.00
        [details] => blabla
        [15] => blabla
        [category] => Computer
        [16] => Computer
        [date_added] => 2012-04-06
        [17] => 2012-04-06
    )
)  

2 个答案:

答案 0 :(得分:0)

问题是您没有从产品表中进行选择。您选择的查询只是orders.*,只不过是orders table下的所有列。解决方案是将产品列添加到您的选择查询中。我不太清楚为什么你明确地使用内连接,我会是这样的:

select orders.*, products.* from orders,order_detail,products where 
orders.oid=order_detail.oid and order_detail.pid=products.pid and orders.kid=1

以下是我得到的结果:

| oid | kid | pid | product_name |
|  1  |  1  |  1  | camera       |
|  1  |  1  |  2  | pants        |

答案 1 :(得分:0)

您的代码使用

进入while循环
$lastoid = 0;

它将运行第一个while循环,因为$ lastoid不等于$ i ['oid']。一个是零,另一个是一个。在下一行中,您将$ i ['oid']的值分配给$ lastoid。这使它们相等,你的while循环将停止。如果循环不会中断,那么您将获得无穷无尽的相同信息。在while循环中没有对下一个订单的引用。

像这样改变

$lastoid = 1;
foreach($orders as $key => $value){

    if($lastoid == $value[$key]['oid']) {
        echo "Ordernr: ".$lastoid."<br/>";
        echo "Produktname: ".$i['product_name']."<br>";
        echo "Menge: ".$i['quantity']."<br/>";
        echo "Preis: ".$i['price']."<br/>";
        echo "Status: ".$i['status']."<br/>";
        echo "<br/><br/><hr/>";    
    } else {
        $lastoid = $value[$key+1]['oid'];
    }
}