简体   繁体   English

希望有一个while循环仅在表中显示数据库的存储列表

[英]Wanting a while loop to only display a list of stores from the database in a table

I am creating a report in php. 我正在用php创建报告。 I have a database that has order data for 20 plus stores. 我有一个数据库,其中包含20多家商店的订单数据。 I am wanting to create a table to display the stores and then list data in the subsequent rows after the store number. 我想创建一个表来显示商店,然后在商店编号后的后续行中列出数据。 I am attempting to do this with a while loop. 我试图用while循环来做到这一点。 The only problem is, there are multiple entries for each store. 唯一的问题是,每个商店都有多个条目。 I am only wanting the report to list the stores once. 我只希望报表一次列出商店。 Currently it loops through and lists all the stores, goes through and lists them again. 当前,它遍历并列出所有商店,遍历并再次列出它们。 I know there has to be a way to have it only pull the data for the store numbers once. 我知道必须有一种方法,使它仅将商店编号的数据提取一次。 Is there a better solution for this instead of the while loop? 有没有比while循环更好的解决方案?

    if ($store_number == '[All_Stores]'){
    $store_query = mysql_query('SELECT * FROM `Orders` WHERE `StoreNumber` <> "0"');
    while($store_row = mysql_fetch_array($store_query)){
        #$store_number = $store_row['StoreNumber'];
        echo    "<tr>";
        echo    "<td align=\"center\"><strong>" . $store_row['StoreNumber'] . "</strong></td>";
and a bunch more data afterwards

Produces a result similar to this except its repeated many times. 产生类似于此的结果,但重复多次。

http://i.imgur.com/9FzZ8Gp.png http://i.imgur.com/9FzZ8Gp.png

First, I would order by store number (maybe store name if its there and makes more sense). 首先,我要按商店编号排序(如果有商店名称,则更有意义)。 Then, when looping through the list, when I ran in to a new store I would print it. 然后,当遍历列表时,当我遇到新商店时,我将其打印出来。 I would only print when I found a new store. 我只有在找到新商店时才打印。

$current_store = null;
while(...) {
     if($current_store != $store_row['StoreNumber']) {
         //print Store
         $current_store = $store_row['StoreNumber'];
     }
}

That type of thing. 那种东西。

Try: 尝试:

if ($store_number == '[All_Stores]'){
        $store_query = mysql_query('SELECT * 
              FROM `Orders` 
              WHERE `StoreNumber` <> "0"
              ORDER BY StoreNumber');
      $old_Nbr = "";
      while($store_row = mysql_fetch_array($store_query)){
        if ( $store_row['StoreNumber'] == $old_Nbr ) {
          $store_number = ""
        } else {
          $store_number = $store_row['StoreNumber'];
          $old_Nbr = $store_number;
        }
        echo    "<tr>";
        echo    "<td align=\"center\"><strong>" . $store_number . "</strong></td>";
    and a bunch more data afterwards

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM