【问题标题】:Auto span row with same content (php/mysql/jQuery)具有相同内容的自动跨行(php/mysql/jQuery)
【发布时间】:2015-03-29 11:39:26
【问题描述】:

我正在将 mysql 中的数据显示到网页表中。
代码(我正在使用 wordpress):

    global $wpdb;
    $Lists = $wpdb->get_results("select * from `price_record`;");
    if($Lists== null)
    {
        echo"There is no price record yet!";
    } 
   else
   {
        echo"<table class='priceTable' style='border:1px;border-collapse:collapse;'>";
            echo"<thead>";
                echo"<tr>";
                    echo"<th>Date</th>";
                    echo"<th>Category</th>";
                    echo"<th>Type</th>";
                    echo"<th>Price</th>";
                echo"</tr>";
            echo"</thead>";
            echo"<tbody>";
            foreach ($Lists as $List) 
            {
                echo"<tr>";
                    echo"<td>".$List->Date."</td>";
                    echo"<td>".$List->Category."</td>";
                    echo"<td>".$List->Type."</td>";
                    echo"<td>".number_format($List->Price,2)."</td>";
                echo"</tr>";
            }
            echo"</tbody>";
        echo"</table>";
    }

当前结果:

Date    Category    Type    Price   
---------------------------------  
30/1    A           T1      12     
30/1    A           T2      13     
30/1    B           T3      10      
30/1    B           T4      11      
29/1    A           T1      11  

预期:

Date    Category    Type    Price   
---------------------------------
30/1    A           T1      12     
                    T2      13     
        B           T3      10      
                    T4      11      
29/1    A           T1      11      

如何在显示表格时自动分组日期、类别和类型?谢谢你。

【问题讨论】:

  • 您能否给我们您一直在处理的代码或数据库的结构,(它只是一个表还是通过一个表连接到另一个表等等)。
  • 只需向我们展示您生成此输出的 php 代码

标签: php jquery mysql html-table


【解决方案1】:

下面的代码应该可以做到。注意sql查询中的ORDER BY很重要。

 global $wpdb;
    $Lists = $wpdb->get_results("select * from `price_record` ORDER BY Date DESC;");
    if($Lists== null)
    {
        echo"There is no price record yet!";
    } 
   else
   {
        // Initiate dateElement
        $dateElement = '';

        echo"<table class='priceTable' style='border:1px;border-collapse:collapse;'>";
            echo"<thead>";
                echo"<tr>";
                    echo"<th>Date</th>";
                    echo"<th>Category</th>";
                    echo"<th>Type</th>";
                    echo"<th>Price</th>";
                echo"</tr>";
            echo"</thead>";
            echo"<tbody>";
            foreach ($Lists as $List) 
            {

                if($List->Date == $dateElement) {

                  // If existing day, show a blank td
                  $dateElement = '&nbsp;';
                } else {

                  // If new day, reset categoryElement and set the dateElement to the new day
                  $categoryElement = '';
                  $dateElement = $List->Date;
                }

            // If same category (and same day), show a blank td, otherwise show the category
            $categoryElement = ($List->Category == $categoryElement) ? '&nbsp;' : $List->Category;

             echo"<tr>";
                    echo"<td>".$dateElement."</td>";
                    echo"<td>".$categoryElement."</td>";
                    echo"<td>".$List->Type."</td>";
                    echo"<td>".number_format($List->Price,2)."</td>";
                echo"</tr>";
            }
            echo"</tbody>";
    echo"</table>";
}

【讨论】:

  • 谢谢你,这几乎适合!它可以跨越具有相同日期的行吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-12
  • 2014-08-20
  • 2013-05-07
  • 1970-01-01
相关资源
最近更新 更多