PHP - 从 html 上的关联数组过滤和输出数据

标签 php mysql associative-array

我有以下从数据库调用的关联数组:

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
        )
  
)

数据库中有三个表(DVD、书籍、家具),它们具有不同的列字段,新产品将被添加到这些表中。我用 UNION ALL 加入它们,以便按时间戳排序(以便新产品出现在产品列表的第一位)。 好吧,我正在按时间戳对数据进行排序,但在将其输出到 HTML 页面时出现了困难。

这是我在 html 上显示数据的代码,但我不知道如何显示每个产品的描述。

<?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
          }
  ?>

即,DVD 产品应显示描述“MB”,书籍应显示“重量”,家具应显示“尺寸”。但此刻我遇到了以下困惑: enter image description here

产品必须展示在方框内,每个产品都有自己的产品描述。同时不按时间戳中断产品的显示。如下例所示:

enter image description here

由于我使用的是 UNION,所以数组中的键名称对于所有产品都是相同的,这使得在输出时调用特定产品变得更加困难。

如何解决这种情况,以便在不破坏时间戳的情况下根据产品描述正确显示产品?

最佳答案

如果我理解您的描述和查询输出,您只需要在针对 origin 的 HTML 测试中使用一些 PHP 条件逻辑:

<?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; ?>

在您的 SQL 查询中,我建议在可变列上使用别名,这样它们就不会全部作为 b_weight 出现。这令人困惑,当您在 2 年内必须更改此代码时,您会感谢自己。

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
  ....

关于PHP - 从 html 上的关联数组过滤和输出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66072528/

相关文章:

initialization - 初始化结构值和字符串键的关联数组

php - 如果值存在于另一个数组中,如何递归过滤数组

php - Wordpress 查询预览?

php - 如何在php中验证请求服务器?

php - 来自 PHP 的 Angular 和日期 InvalidPipeArgument : 'Unable to convert "[object Object ]"into a date' for pipe 'DatePipe'

Python MySQL 和 Django 问题

mysql - 如何在 mysql workbench 中获取现有模式的脚本

php - 将值从 MySql 传递到 PHP。如何避免NULL值

php - 在 AWS 中查找我的实例区域

mysql - 错误代码: 1054 Unknown column 'try.tb_stores.division' in 'on clause'