PHP - filter and output data from an associative array on html

Viewed 124

I have the following associative array which is called from the database:

Array
(
    [0] => Array
        (
            [id] => 81
            [book_sku] => BokFoxME
            [book_name] => BokFoxME
            [book_price] => 123.4
            [b_weight] => 123
            [f_width] => 
            [f_length] => 
            [post_time] => 2021-02-05 04:02:30
            [origin] => book
        )

    [1] => Array
        (
            [id] => 234
            [book_sku] => TESTDVD
            [book_name] => TESTDVD
            [book_price] => 323
            [b_weight] => 12
            [f_width] => 
            [f_length] => 
            [post_time] => 2021-02-02 00:23:04
            [origin] => dvd
        )

    [3] => Array
        (
            [id] => 41
            [book_sku] => Мебель
            [book_name] => Мебель
            [book_price] => 21
            [b_weight] => 21
            [f_width] => 21x
            [f_length] => 21
            [post_time] => 2021-01-31 21:16:52
            [origin] => furniture
        )
  
)

There are three tables in the database (DVD, book, furniture) with different column fields to which new products will be added. I joined them with UNION ALL in order to sort by timestamp (so that the new product appears first in the product list). Well I'm getting the data sorted by timestamp, but my difficulty arises when outputting it to HTML page.

This is my code on html which displays data, but I cannot figure out how to display the description of each product.

<?php
    // $dvds = array_filter($data_dvd, function ($item) {
    //     return $item['origin'] === 'dvd';
    // });

    foreach((array)$data_dvd as $prod){
?>

<!-- Fetching DVD -->
  <div class="card" style="width: 12rem; height: 12rem;">
      <div class="card-body">
          <label class="checkbox-inline">
          <input  name="box[]" type="checkbox" value="<?php echo $prod["id"];?>">
          </label>
      <br>
      <div class="cardt text-center">
          <p class="card-title"><b><?php echo $prod["book_sku"];?></b></p>
          <p class=""><?php echo $prod["book_name"]; ?></p>
          <p class=""><?php echo $prod["book_price"]; ?>$</p>
          
          <!-- the problem occurs with this part of the code -->
          <p>Size: <?php echo $prod["b_weight"]; ?> MB </p>
          <p>Size: <?php echo $book["b_weight"]; ?> Weight </p>
          <p>Dimension:<br><small> <?php echo $prod["b_weight"]; ?> x <?php echo $prod["f_width"]; ?> x <?php echo $prod["f_length"]; ?></small></p>

      </div>
      </div>
  </div>


  <?php
          }
  ?>

That is, the description "MB" should be displayed for the DVD product, "Weight" for the book and "Dimension" only for the furniture. But at the moment I'm getting the following mess: enter image description here

Products must be displayed in boxes each product with its own product descriptions. While not disrupting the display of products by timestamp. As in the example below:

enter image description here

Since I am using UNION, the key name in the array is the same for all products, and it makes it harder to call a specific product on output.

How can I solve this situation so that the products are displayed correctly according to the product descriptions without breaking the timestamp?

1 Answers

If I understand your description and your query output, you just need a little bit of PHP conditional logic in your HTML testing for the origin:

<?php if ($prod["origin"] == "dvd"): ?>
<p>Size: <?php echo $prod["b_weight"]; ?> MB </p>

<?php elseif ($prod["origin"] == "book"): ?>
<p>Size: <?php echo $prod["b_weight"]; ?> Weight </p>

<?php elseif ($prod["origin"] == "furniture"): ?>
<p>Dimension:<br><small> <?php echo $prod["b_weight"]; ?> x <?php echo $prod["f_width"]; ?> x <?php echo $prod["f_length"]; ?></small></p>

<?php else: ?>
<p>unknown type or skip this </p>
<?php endif; ?>

In your SQL query, I would recommend using an alias on the varying column, so they don't all come out as b_weight. That is confusing and you will thank yourself later when you have to change this code in 2 years.

SELECT
  id,
  -- Give the column you "overload" an alias that applies to all 3
  book_sku AS sku,
  b_weight AS measure,
  ...
UNION ALL
SELECT
  id,
  dvd_sku AS sku,
  dvd_mb AS measure,
  ...
UNION ALL
SELECT
  id,
  furniture_sku AS sku,
  furniture_weight AS measure
  ....
Related