简体   繁体   中英

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

I am creating a report in php. I have a database that has order data for 20 plus stores. 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. 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?

    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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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