dtwxt88240 2017-01-05 11:14
浏览 100

mysqli php问题显示组中的订单/ SELECT DISTINCT只显示一个结果

I'm having some trouble with my php / mysqli code, hopefully you can help me. I'm currently working on an online shop for a school project. Customers are able to buy things, they get an order number and I'm writing their user_id, the order number, the different products and some other things in a relation. now the administrator should be able to see all orders. right now it looks like this (I copied my table into a word table, so it's easier to see the structure):

part of the table

So the problem is that I have two different order numbers (80425 and 14808) and I want to show each number (and the name and adress of the custumer, too) only one time, but for each order number all different ordered products. I imagine it like this:

part of the table (more organised)

(it's german, I hope you still get what I mean) So this is the code right now for getting all the information and show them in a table:

$selection = "SELECT * FROM kundenbestellungen, zahlart, produkte, user, status_bestellung, wohnsitz, kontodaten
                WHERE b_zahlung_id = z_id
                AND b_produkte_id = p_id
                AND b_user_id = u_id
                AND b_status_id = sb_id
                AND w_user_id = u_id
                AND d_user_id = u_id";
$sql = mysqli_query ($dblink, $selection) OR die (mysqli_error($dblink));
if (mysqli_num_rows ($sql) > 0) {
    while ($row = mysqli_fetch_assoc($sql)) {
    ?>
    <tr>
        <td>
            <?php /*Change the status to sent*/  
            if ($row['b_status_id'] == '0') {
                echo $row['sb_status'];
                ?>
                <form action="admin-bestellungen.php" method="POST">
                    <input type="hidden" name="id" value="<?php echo $row['b_id']?>">
                    <input type="submit" name="versenden" value="versenden">
                </form>
            <?php
            } else {
                echo $row['sb_status'];
            }
            ?>
        </td>
        <td> <?php echo $row['b_nummer'];?></td>
        <td><?php echo $row['u_vorname']." ".$row['u_nachname'];?></td>
        <td><?php echo $row['p_produktname'];?></td>
        <td><?php echo $row['b_menge_produkt'];?></td>
        <td><?php echo $row['b_einzelpreis'];?></td>
        <td><?php echo $row['z_art'];?></td>
        <td><?php echo $row['b_zeitpunkt'];?></td>
    </tr>
    <?php
    }
}

I'm really confused. I tried this below the $selection part, just to start with something:

$anzahl_bestellungen = "SELECT COUNT(DISTINCT b_nummer) AS nr FROM kundenbestellungen";
$anzahl_bestellungen = mysqli_query ($dblink, $anzahl_bestellungen) OR die (mysqli_error($dblink));
$bestell = mysqli_fetch_array($anzahl_bestellungen);

print_r($bestell['nr']);

and the code counts the amount of the different order numbers (8). But if I use it without COUNT, it shows only the first order number (80425) and also counts only 1 result and doesn't get the other 7 numbers.

$anzahl_bestellungen = "SELECT DISTINCT b_nummer FROM kundenbestellungen";
$anzahl_bestellungen = mysqli_query ($dblink, $anzahl_bestellungen) OR die (mysqli_error($dblink));
$bestell = mysqli_fetch_array($anzahl_bestellungen);

print_r($bestell['b_nummer']);
$b = count($bestell['b_nummer']);
echo "<br>".$b;

I also tried to work something out with GROUP, but then the code shows only one item for each order number. I tried to work with a for-loop as well, but that didn't work out either. I thought about a multidimensional array, but I wasn't able to think through that whole thing, I'm not very good at php / mysqli. So I have no idea how to go on. Maybe you can help me. This is my first question, so please let me know if I need to be more specific or you need more code or anything. thanks a lot!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
    • ¥15 unity连接Sqlserver
    • ¥15 图中这种约束条件lingo该怎么表示出来
    • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
    • ¥15 流式socket文件传输答疑
    • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
    • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
    • ¥15 win10,这种情况怎么办
    • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
    • ¥100 在连接内网VPN时,如何同时保持互联网连接