【问题标题】:Display mysql table in HTML, merge all rows that has same values in second column在 HTML 中显示 mysql 表,合并第二列中具有相同值的所有行
【发布时间】:2015-10-15 13:12:09
【问题描述】:

我有以下 php 代码,将 mysql 表显示为 html 表。 Mysql 表包含 4 列:日期、id、项目、数量。

在 html 表格中显示时,我想合并第二列中具有相同“id”的所有行。

非常感谢任何帮助。

$sql = "SELECT * FROM table_name";
$result = mysqli_query($db, $sql);

if ($result==True) 
{   
    echo "<table border='3' style='position:absolute;width:60%;top:20px;left:20%;text-align:center;border-collapse: collapse;'>";

    echo "<tr>";
        echo "<th>Date</th>";
        echo "<th>id</th>";
        echo "<th>item</th>";
        echo "<th>quantity</th>";       
    echo "</tr>";

    while($row = mysqli_fetch_row($result))
    {
        echo "<tr>";            
            echo "<td>$row[1]</td>";
            echo "<td>$row[2]</td>";
            echo "<td>$row[3]</td>";
            echo "<td>$row[4]</td>";            
        echo "</tr>";
    }

    mysqli_close($db);
} 

Mysql 中的表如下所示:

    Date         Id     Item    Quantity
    1/1/2015    12345   Apple   1 KG
    1/1/2015    12345   Apple   1 KG
    1/1/2015    12345   Apple   1 KG
    1/1/2015    97786   Apple   1 KG
    1/1/2015    76767   Apple   1 KG
    1/1/2015    24335   Apple   1 KG
    1/1/2015    9098    Apple   1 KG

我希望我的 html 表格如下所示: (第 1、2 和 3 行被合并,因为它具有相同的 id)

    Date         Id     Item    Quantity
    1/1/2015            Apple   1 KG
    1/1/2015    12345   Carrot  1 KG
    1/1/2015            Onion   1 KG
    1/1/2015    97786   Apple   1 KG
    1/1/2015    76767   Apple   1 KG
    1/1/2015    24335   Apple   1 KG
    1/1/2015    9098    Apple   1 KG

【问题讨论】:

标签: php html mysql


【解决方案1】:

因为它似乎不是最好的解决方案,但它应该可以工作..try

$check=array();

while($row = mysqli_fetch_row($result))
    {
      if(in_array($row[2],$check)
        $row[2]="";
        echo "<tr>";            
            echo "<td>$row[1]</td>";
            echo "<td>$row[2]</td>";
            echo "<td>$row[3]</td>";
            echo "<td>$row[4]</td>";            
        echo "</tr>";
        $check[]=$row[2];
    }

【讨论】:

    【解决方案2】:

    如果您不介意在数据中循环额外的时间(和一点),您可以使用 rowspan 来完成。

    $table = array();
    while($row = mysqli_fetch_row($result)) {
        $table[$row[1]][] = array($row[0],$row[2],$row[3]);
    }
    
    echo "<table border='3' style='position:absolute;width:60%;top:20px;left:20%;text-align:center;border-collapse: collapse;'>";
    echo "<thead>";
        echo "<tr>";
            echo "<th>Date</th>";
            echo "<th>id</th>";
            echo "<th>item</th>";
            echo "<th>quantity</th>";
        echo "</tr>";
    echo "</thead>";
    echo "<tbody>";
    foreach($table as $id=>$idRows) {
        $rowspan = count($idRows);
        for($i = 0; $i < $rowspan; $i++) {
            echo "<tr>";
                echo "<td>{$idRows[$i][0]}</td>";
                if($i === 0)
                    echo "<td rowspan={$rowspan}>{$id}</td>";
                echo "<td>{$idRows[$i][1]}</td>";
                echo "<td>{$idRows[$i][2]}</td>";
            echo "</tr>";
        }
    }
    echo "</tbody>";
    echo "</table>";
    

    【讨论】:

      【解决方案3】:
      I have updated my answer. Please see it and try
      
      
      //Set all value in some temp array 
      $array_date = "";
      $array_id = "";
      $array_item = "";
      $array_qty = "";
      $array_rowspan = "";
      
      $i=0;
      while($row = mysqli_fetch_row($result))
      {
      
      
          if($tempid != $row[1]){
      
              $tempid = $row[1];
              $rowspan = 1;       
              $array_date[] = $row[0]; 
              $array_id[] = $row[1];
              $array_item[] = $row[2];
              $array_qty[] = $row[3];         
              $array_rowspan[$i] = $rowspan;
      
              //Set another temp value to set same rowspan in mached category id  
              $same_id=1; 
          }else{
      
              $rowspan++;     
              $array_date[] = $row[0];
              $array_id[] = $row[1];
              $array_item[] = $row[2];
              $array_qty[] = $row[3];
      
              //Set another temp value to set same rowspan in mached category id          
              $same_id++;     
              for($j=0; $j<$same_id; $j++){
                  $array_rowspan[$i-($j)] = $rowspan;
              }
      
          }       
      
          $i++;   
      
      }
      
      echo "<table border='1' style='position:absolute;width:60%;top:20px;left:20%;text-align:center;border-collapse: collapse;'>";
      
          echo "<tr>";
              echo "<th>Date</th>";
              echo "<th>id</th>";
              echo "<th>item</th>";
              echo "<th>quantity</th>";       
           echo "</tr>";
          //Set a temp variable like for id
          $tempid2 = "";
          foreach($array_id as $key => $val){
      
              if($tempid2 != $val){
                  $tempid2 = $val;
                  echo "<tr>"; 
                  echo "<td>".$array_date[$key]."</td>";
                  echo "<td rowspan='".$array_rowspan[$key]."'>".$array_id[$key]."</td>";     
                  echo "<td>".$array_item[$key]."</td>";
                  echo "<td>".$array_qty[$key]."</td>"; 
                  echo "</tr>";
              }else{
                  echo "<tr>"; 
                  echo "<td>".$array_date[$key]."</td>";              
                  echo "<td>".$array_item[$key]."</td>";
                  echo "<td>".$array_qty[$key]."</td>"; 
                  echo "</tr>";
              }
          }
      
       echo "</table>";
      

      请试试这个

      【讨论】:

      • 这将打印第一个 id,如果下一个 id 与第一个相同,则它不会打印任何内容。我说的对吗?
      【解决方案4】:

      使用 group by 和列名

      $sql = "SELECT * FROM table_name GROUP BY `id`";
      

      【讨论】:

      • 如果我们这样做,我们只会得到一行。
      • 然后试试$sql = "SELECT * FROM table_name GROUP BY id, item";
      猜你喜欢
      相关资源
      最近更新 更多
      热门标签