显示数据库中具有相同ID的条目

时间:2013-03-15 23:21:02

标签: php mysql arrays while-loop

我有一个脚本,允许管理员查看客户订单,他们可以决定查看订单详细信息或删除订单。用户可以订购一个或多个项目,数据库中的条目使用相同的order_id创建,但具有不同的product_id。当我显示我的订单时,这些重复的条目会显示出来,但我只想为每个order_id输入一个条目。以下是我的功能

function viewOrdersAdmin(){
//This block grabs the orders
$order_list = "";
//Selecting all the orders in the table from that member
$sql = mysql_query("SELECT * FROM `transactions`") or die(mysql_error());

while ($transactions = mysql_fetch_array($sql)) {
    //creating variables from the information
    $order_id = $transactions["order_id"];
    $mem_id = $transactions["mem_id"];

    $order_details = mysql_query("SELECT * FROM `transactionDetails` WHERE `order_id` = $order_id") or die(mysql_error());
    $orderDetailsCount = mysql_num_rows($order_details);
    while ($row = mysql_fetch_array($order_details)) {
        //creating variables from the information
        $order_product_id = $row["Product_ID"];

        $member_details = mysql_query("SELECT * FROM `members` WHERE `mem_id` = $mem_id") or die(mysql_error());
        $memberDetailsCount = mysql_num_rows($member_details);
        while ($row2 = mysql_fetch_array($member_details)) {
            //creating variables from the information
            $order_mem_fname = $row2["mem_first_name"];
            $order_mem_lname = $row2["mem_last_name"];

        $order_list .= "Order ID:$order_id - Customer Name: $order_mem_fname $order_mem_lname  &nbsp; &nbsp; &nbsp; <a href='manage_order.php?orderid=$order_id'>View</a> &bull; <a href='manage_orders.php?deleteid=$order_id'>Delete</a><br/>";

        }
    }
}

if (count($orderDetailsCount) == 0) {
    $order_list = "You have no orders to display";
}

print_r($order_list);
}

1 个答案:

答案 0 :(得分:1)

SELECT *
FROM transactions
GROUP BY order_id