dqnf28092 2017-06-08 12:21
浏览 13
已采纳

在列中放置多个MySQL Row

the problem that i've is, that I have a Table in my Database and want to fetch some of the rows of this table in one column.

For example I've this table names klz:

|-------+-----------+-----+-----|
| ID    | Name      | LNr | LID |
|-------+-----------+-----+-----|
| 1     | 0000_01   | 1   | 16  |
| 2     | 0000_01   | 2   | 35  |
| 3     | 0000_02   | 1   | 16  |
| 4     | 0000_02   | 2   | 35  |
| 5     | 0000_10   | 1   | 18  |
| ..    | ..        | ..  | ..  |
| 297   | 0214_01   | 1   | 23  |
| 298   | 0214_01   | 1   | 66  |
| 299   | 0214_01   | 2   | 24  |
| 300   | 0214_01   | 2   | 67  |
| 301   | 0214_01   | 3   | 26  |
| 302   | 0214_01   | 4   | 28  |
| 303   | 0214_01   | 4   | 69  |
| 304   | 0214_01   | 5   | 30  |
| 305   | 0214_01   | 5   | 70  |
| 306   | 0214_01   | 6   | 31  |
| 307   | 0214_01   | 6   | 71  |
|-------+-----------+-----+-----|

If I fetch this table with a while-loop in PHP I would get the same table.

So what I want to have is a table like this:

|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
| Name      | LNr1    | LNr2    | LNr3    | LNr4    | LNr5    | LNr6    | LNr7    | LNr8    | LNr9    | LNr10   |
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|
| 0000_01   | 16      | 35      |         |         |         |         |         |         |         |         |
| 0000_02   | 16      | 35      |         |         |         |         |         |         |         |         |
| 0000_10   | 18      |         |         |         |         |         |         |         |         |         |
| 0214_01   | 23 - 66 | 24      | 26 - 68 | 28 - 69 | 30 - 70 | 31 - 71 |         |         |         |         |
|-----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------|

This table have the column Name and an enumeration of the possible LNr from 1 to 10 as columns. The data inside the LNr columns arethe LIDs from the table above.

My Question is, how can I put in dependency of the Name and the LNr, the LIDs in the right columns? In this table you have listed all Names once and set for all LNr the right LID.

Here is the Code I'm trying to do this with... Until now it works only for the first LNr column, to write the LID in:

<?php
  include "dbconnect.php";

  $klz = mysqli_query($db, "SELECT      *
                            FROM        klz;");  

?>  
  <div class="container-fluid">
  <div class="row">
    <div class="table-responsive">
    <div class="col-xs-12">
    <table id="grid-klz" class="table table-condensed table-hover table-striped">
      <thead>   
        <tr>
          <th> Name </th>
          <th> Leitungsnr 1</th>
          <th> Leitungsnr 2</th>
          <th> Leitungsnr 3</th>
          <th> Leitungsnr 4</th>
          <th> Leitungsnr 5</th>
          <th> Leitungsnr 6</th>
          <th> Leitungsnr 7</th>
          <th> Leitungsnr 8</th>
          <th> Leitungsnr 9</th>
          <th> Leitungsnr 10</th>
        </tr>  
      </thead>

    <tbody>
<?php         
  $name_old = ""; 
  $lnr_old  = "";
  $name     = []; 
  $lnr      = [];
  $lid      = [];

  while($row = mysqli_fetch_array($klz, MYSQL_ASSOC)){
    $name[] = $row['Name'];
    $lnr[] = $row['LNr'];
    $lid[] = $row['LID'];
  }     

  for($i=0; $i <= sizeof($name)-1; $i++){

    if($name[$i] != $name_old){

      echo "<tr>";
        echo "<td>". $name[$i] . "</td> 
";

        if($lnr[$i] != $lnr_old){
          echo "<td>". $lid[$i] . "</td> 
";

          $lnr_old != $lnr[$i];
        }

      echo "</tr>";

      $name_old = $name[$i];
    }
  }    
?>
    </tbody>
  </table>

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

<?php  
  mysqli_close($db);
?>

I hope you know what I mean and trying to do. If not feel free to ask please. Thank you

  • 写回答

3条回答 默认 最新

  • dpl9717 2017-06-08 12:40
    关注

    Start with this query

    SELECT
        Name AS tableRows,
        LNr AS tableCols,
        GROUP_CONCAT(LID ORDER BY LID SEPARATOR ' - ') AS cellValue
    FROM klz
    GROUP BY 1,2
    ORDER BY 1,2;
    

    Now put the results in an array. Also, get every distinct column (LNr) and row (Name) in other arrays.

    $ar = array();
    $tableRows = array();
    $tableCols = array();
    
    while($row = mysqli_fetch_array($klz, MYSQL_ASSOC)){
        $ar[$row['tableRows']][$row['tableCols']] = $row['cellValue'];
        $tableRows[] = $row['tableRows'];
        $tableCols[] = $row['tableCols'];
    }
    
    $tableRows = array_unique($tableRows);
    $tableCols = array_unique($tableCols);
    

    sort the 2 arrays with rows and columns

    sort($tableRows);
    sort($tableCols);
    

    foreach row, foreach column, echo value of the multi array

    echo "<table>";
    echo "<tr><th>Name</th>";
    
    foreach($tableCols as $x){
        echo "<th>LNr$x</th>";
    }
    
    echo "</tr>";
    
    foreach($tableRows as $y){
        echo "<tr><td>$y</td>";
        foreach($tableCols as $x){
            echo "<td>" . $ar[$y][$x] . "</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题