-1

I would like to know if it is possible to categorize data in groups when echoing it .

For example if I have a table of different phones and separate them by brand, i.e. Apple, Samsung etc, and I want the data to be displayed with a header of the brand of phone and under would have the phone type.

Apple
iphone 4
iphone 4s
iphone 5

Samsung
Galaxy Note 2
Galaxy S4

<div id="wrap">
    <table>
        <thead>
            <tr>
                <th><?php echo phones['brand']; ?></th>
                <th>Price</th>
            </tr>
        </thead>

        <tbody>
            <?php foreach($phones as $phone): ?>
                <tr>
                    <td><?php echo $phone['name']); ?>
                        <br>
                    <ul>
                        <li><?php echo $phone['price']); ?></li>
                    </ul>
                    </td>
                    <td>$<?php echo $phone['released']); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

This is what I have so far, sorry for the wait.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
YABOI
  • 29
  • 1
  • 6

2 Answers2

0

Here's a really basic example. Assuming you have your phones in an array like this:

$phones = [
    ['Apple',  'iphone 4'],
    ['Apple','iphone 4s'],
    ['Apple','iphone 5'],
    ['Samsung','Galaxy Note 2'],
    ['Samsung','Galaxy S4']
];

You just keep track of the brand as you output them, and whenever the brand is different from the previous brand, output a header for it.

$previous_brand = null;
foreach ($phones as $phone) {
    list($brand, $model) = $phone;
    if ($brand != $previous_brand) {  // check if current brand is different than previous
        echo "<h3>$brand</h3>";
    }
    echo "$model<br>";
    $previous_brand = $brand;     // current brand becomes previous brand for next iteration
}

I kept the HTML minimal so it wouldn't be distracting from the working part. Obviously you can add whatever markup you need.

The important thing with this approach is that it totally depends on your array being sorted by brand, so if this is coming out of your database ORDER BY brand, or if it's some other source, use one of the various PHP sort functions to get it in that order first.

Community
  • 1
  • 1
Don't Panic
  • 37,589
  • 9
  • 55
  • 71
  • My phones are in a mySQL database, would I have to do something different in the beginning? – YABOI Dec 22 '16 at 18:57
  • Probably not much different. Just select all the phones you want to display, but be sure to add `ORDER BY brand`. – Don't Panic Dec 22 '16 at 19:01
  • Thank you for the answer. How about echoing it in table form? – YABOI Dec 22 '16 at 19:11
  • That really depends on exactly how you want your table to be output. If you want a separate table for each brand it would be different than if you wanted them all in the same table separated by a differently formatted row or something like that. I intended the answer to be more of an example of how the PHP part works. – Don't Panic Dec 22 '16 at 19:47
0

First do

$phonesByBrand = [];

foreach ($phones as $phone)
{
  $phonesByBrand[$phone['brand']][] = $phone;
}

Now in your view, you can do

<?php foreach($phonesByBrand as $brand => $phones): ?>
  <div id="wrap">
    <table>
      <thead>
        <tr>
          <th><?php echo $brand; ?></th>
          <th>Price</th>
        </tr>
      </thead>

      <tbody>
        <?php foreach($phones as $phone): ?>
          <tr>
            <td>
              <?php echo $phone['name']); ?>
              <br>
              <ul>
                <li><?php echo $phone['price']); ?></li>
              </ul>
            </td>
            <td>
              $<?php echo $phone['released']); ?>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
<?php endforeach; ?>
prateekkathal
  • 3,102
  • 1
  • 17
  • 37