2016-04-10 1 views
0

Mes tableaux sont les suivantes:PHP, MySQL Junction Table affiche des informations de manière incorrecte

PERSON2: personID validPositions: positionID, positionDesc personPositions: personID, positionID

Je veux être en mesure d'afficher une personne leurs positions multiples (certains peuvent seulement avoir une position) sur une ligne. Exemple: Positions de Sierra: s1, s2

Actuellement, il affiche chaque position qu'ils ont, mais sur des lignes différentes ET il répète deux fois la dernière position dans la base de données. Exemple:

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>'; 

Toute aide est grandement appréciée, je suis nouveau sur PHP et MySQL et je ne sais pas trop quoi faire!

EXEMPLE DE DONNÉES:

table personPositions:

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

Table validPositions:

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

Répondre

0

Quelque chose comme cela devrait fonctionner pour vous:

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 

Sortie:

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

Demo here

+0

qui affiche la position vide (s): ________ Pour chaque personne, il y a 4 positions vides, je suppose parce qu'il ya 2 personnes actuellement dans mon table, avec 2 positions chacune ??? –

+0

@JAdams Pouvez-vous poster des exemples de données? –

+0

Je l'ai ajouté en haut - je ne pouvais pas comprendre comment le formater correctement, cependant, j'espère qu'il est lisible –