2016-06-21 4 views
0

私はこのRAWのテーブルがあります。異なるデータを複数の列で1つのテーブル行に結合するにはどうすればよいですか?

 
==================================================== 
| id | name  | fieldid | info1  | info2  | 
==================================================== 
| 1 | testname1 | 1  | testing 1 | testing 2 | 
|----|-----------|---------|-----------|-----------| 
| 2 | testname2 | 2  | testing 3 | testing 4 | 
|----|-----------|---------|-----------|-----------| 
| 3 | testname2 | 2  | testing 5 | testing 6 | 
==================================================== 

を、私はこのような何かを見て、テーブルのための基準としての「name」と「フィールド識別子」を使用したい:

 
======================================= 
| id | name  | fieldid | info  | 
======================================= 
| 1 | testname1 | 1  | testing1 | 
| |   |   | testing2 | 
|----|-----------|---------|----------| 
| 2 | testname2 | 2  | testing3 | 
| |   |   | testing4 | 
| |   |   | testing5 | 
| |   |   | testing6 | 
======================================= 



マイコードは現在、これを生成します。

 
========================================= 
| id | name  | fieldid | info  | 
========================================= 
| 1 | testname1 | 1   | testing1 | 
| |   |   | testing2 | 
|----|-----------|-----------|----------| 
| 2 | testname2 | 2   | testing3 | 
| |   |   | testing4 | 
| |   |   | testing6 | 
|----|   |-----------|----------|------------ 
| 3 |   | testname2 | 2  | testing 5 | 
| |   |   |   | testing 4 | 
| |   |   |   | testing 6 | 
===================================================== 

は、ここでは、コードです:

<?php $counter = null;?> 
    <?php 
     $code1 = "SELECT * FROM tableq"; 
     $query1 = $conn->query($code1) or trigger_error($conn->error.[$code1]); 

     echo (" 
      <table> 
       <tr> 
        <th>ID</th> 
        <th>Name</th> 
        <th>Field ID</th> 
        <th>Info</th> 
       </tr> 
     "); 

     if($query1->num_rows > 0){ 
      while($row1 = $query1->fetch_assoc()){ 
       echo (" 
        <tr> 
         <td>".$row1['id']."</td> 
       "); 

       if($counter == ""){ 
        $counter = "1"; 
       } 
       else if($counter == "4"){ 
        $counter = $counter/2; 
       } 

       $code2 = "SELECT * FROM tableq WHERE name='" . $row1['name'] . "' AND fieldid='" . $row1['fieldid'] . "'"; 
       $query2 = $conn->query($code2) or trigger_error($conn->error.[$code2]); 

       if($query2->num_rows > 0){ 
        echo (" 
          <td rowspan=\"".$counter."\">".$row1['name']."</td> 
        "); 
       } 

       echo (" 
         <td>".$row1['fieldid']."</td> 
         <td>".$row1['info']." 
       "); 

       if($query2->num_rows > 0){ 
        while($row2 = $query2->fetch_assoc()){ 
         $counter += 1; 
         echo (" 
           <br>".$row2['info2']." 
         "); 
        } 
       }else{ 
        die("No results found"); 
       } 
       echo (" 
         </td> 
        </tr> 
       "); 
      } 
     } 
    ?> 

私は関連する質問を読んだが、私はうまくいっていない。

答えて

0

私は別の方法でそれに取り組むことをお勧めします。あなたは、配列がすでに組み込まれていたら、あなたがやる

最初の事は、それはHTMLのテーブルで出力する方がはるかに簡単だ、

$entries[1][testName1][fieldId] = [entry1, entry2, entry3]; 
$entries[2][testName2][fieldId] = [entry1, entry2, entry3]; 
$entries[3][testName2][fieldId] = []; // "Not found" 

のような多次元配列を構築することです。デバッグする方がはるかに簡単です。

+0

どうすればいいですか/これはどうすればいいですか?私は配列ではあまりよくありません。 – AldwinB

関連する問題