Currently displaying items as company-->all items. how to sort by size as well?

Hi

I currently have the following code:

    $group = array();
        while ($row = mysqli_fetch_assoc($result)) {
        $group[ $row['company'] ][] = $row;
        }
        foreach ($group as $company => $items) {
                echo "$company<br/>";
                foreach ($items as $item) {
                        $image = "item_img_url_goes_here";
                        echo "<img src='$image'/>";
                        $item_name = $item['item'];
                        $item_size = $item['item_size'];
                        $item_version = $item['item_version'];
                        echo "<p>$item_name - $item_size - $item_version</p>";
                }
            echo '<div class="clear"></div>';
        }

which outputs like this:

COMPANY 1
item - item size - item version
item - item size - item version
item - item size - item version

COMPANY 2
item - item size - item version
item - item size - item version
item - item size - item version

But I would like it to output like this:

COMPANY 1
item size one
item - item version
item - item version
item - item version
item size two
item - item version
item - item version
item - item version
etc

COMPANY 2
item size one
item - item version
item - item version
item - item version
item size two
item - item version
item - item version
item - item version
etc

How would I begin about doing that?

Change the query to sort it by company then item size then item/item version, loop through the results and echo them, noting each time the company or item size changes so you can display the new heading / title.

Thank you for your reply. I have changed the query to order by company->size->item etc. The part that is hard for me to understand how to achieve (I am a php novice) is then sorting via size under each company.

Sorting all items via company was pretty easy to get going… but to sort for something under something else… would I just add to my current code, or would I completely need to re-write/re-do the way I have written it?

Well, as long as the query specifies all the orders in the correct sequence, they should all come out. I don’t know your table or column names but something like

select * from items order by company, item_size, item, item_version

Then in your code:

$lastcomp = "";
while ($row = $sql->fetch()) {
  if ($row['company'] != $lastcomp) { 
    // display the company name
    echo "company name";
    $lastsize = ""; // clear this here so it's always shown on a new company
    }
  if ($row['item_size'] != $lastsize) {
    //display the item size
    echo "item size";
    }
  // display the item and item version from the row
  echo "whatever...";
  $lastcomp = $row['company'];
  $lastsize = $row['item_size'];
  }  // end of while() loop

So the loop will remember the company and item size from the previous record, and when the current record is different you can display that information.

Thank you so much. That makes a lot of sense and works really well. Have adapted it to my layout etc and it looks great.

Thanks once more!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.