2016-04-10 9 views
0

私のテーブルがある情報が表示されますそれらの複数の位置(一部は1つの位置のみを持つことができる)を1つの行に表示します。例:Sierraの位置:s1、s2PHP、MySQLのジャンクション表は次のように誤っ

現在は、それぞれの位置が異なる行に表示され、データベースの最後の位置が2回繰り返されます。例:

Sierra's Positions: S1 
Sierra's Positions: S2 
Sierra's Positions: S2 


    $sql = "SELECT * FROM person2"; 
    // LEFT JOIN validTitles ON personTitle.positionID=validTitles.positionID GROUP BY person2.personID"; 

    if ($result = mysqli_query($connection, $sql)) {  
     // loop through the data 

     //create 4 columns for the table 
     $columns=5; 
     $i = 0; 


     while($row = mysqli_fetch_array($result)) 
     { 
      // the % operator gives the remainder of a division of two values 
      // in this case, 0/4, this tells the table row to jump to a new row 
      //  if there are already 4 columns in one row 
      if($i % $columns == 0){ 

       //begin table row 
       echo "<tr>"; 
      } //end if 

      echo '<td class="staffImage badgeText frameImage displayInLine"> 
        <a href=#openModal'.$row["personID"].'><img src="images/staff/'.$row["imgName"].'.jpg"></a><br> 
        <strong>'.$row["firstName"].'</strong> 
        <strong>'.$row["lastName"].'</strong><br>' 
        .$row["position"]. 

     '<div id="openModal'.$row["personID"].'" class="modalDialog"> 
      <div> 
       <a href="#close" title="Close" class="close">X</a> 
       <h2>' . $row["firstName"] . " " . 
       $row["lastName"].'</h2><br> 
       <img class="floatLeft" src="images/staff/'.$row["imgName"] .'.jpg"> 
       <p><strong>Hire Date: </strong>'.$row["hireDate"].'<br> 
       <p><strong>Major: </strong>'.$row["major"].'<br>'; 


        //if the field "major2" (Double Major) is not null, display it 
        if($row["major2"] != NULL) 
        { 
         echo ' & '.$row["major2"].'<br>'; 
        } 

        //if the field "minor" is not null, display it 
        if($row["minor"] != NULL) 
        { 
         echo '<p><strong>Minor: </strong>'.$row["minor"].'<br>'; 
        } 

        //if the field "concentration" is not null, display it 
        if($row["concentration"] != NULL) 
        { 
         echo '<p><strong>Concentration: </strong>'.$row["concentration"]; 
        } 


    **$sql2 = "SELECT * FROM personPositions LEFT JOIN validPositions ON personPositions.positionID=validPositions.positionID "; 

    if ($result2 = mysqli_query($connection, $sql2)) { 
     // loop through the data 

     while($row2 = mysqli_fetch_array($result2)) 
     { 
      echo '<p><strong>Position(s): </strong>'.$row2["positionDesc"]; 
     }//end second while** 

    } //end second if 

      '</div> 
     </div> 

    </div> ';  
       echo '</td>'; 
すべてのヘルプは大歓迎です

は、私が何をすべきか、PHPとMySQLに新しいとわかりませんよ!

サンプルデータ:

personPositionsテーブル:

personID 1 | positionID 11 
personID 1 | positionID 22 
personID 2 | positionID 22 
personID 2 | positionID 55 

validPositionsテーブル:

positionID 11 | positionDesc S1 
positionID 22 | positionDesc S2 
positionID 55 | positionDesc S3 

答えて

0

このような何かがあなたのために働く必要があります。

SELECT p.personID, GROUP_CONCAT(DISTINCT positionDesc ORDER BY positionDesc) AS positions 
FROM person AS p 
LEFT JOIN personPositions AS pp ON p.personID = pp.personID 
LEFT JOIN validPositions AS vp ON pp.positionID = vp.positionID 
GROUP BY p.personID 

出力:

personID | positions 
----------+----------- 
1   | S1,S2 
2   | S2,S3 

Demo here

+0

空白の位置(複数可)が表示されます。現在、私の中の2人がいるので、一人一人のために________ を、4つの空白の位置がありますが、私が想定していますテーブル、それぞれ2つの位置? –

+0

@JAdamsサンプルデータを投稿できますか? –

+0

私はそれを上に追加しました - 私はそれを正しくフォーマットする方法を理解できませんでしたが、それが可読であることを願って –

関連する問題