我的表格如下:
person2: personID validPositions: positionID, positionDesc personPositions: personID, positionID
我希望能够在一行上显示一个人的多个职位(有些可能只有一个职位)。示例:Sierra 的职位:s1, s2
目前它显示他们拥有的每个位置,但是在不同的行上,并且它重复数据库中的最后一个位置两次。例子:
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 的新手,不知道该怎么做!
样本数据:
人员职位表:
personID 1 | positionID 11
personID 1 | positionID 22
personID 2 | positionID 22
personID 2 | positionID 55
有效职位表:
positionID 11 | positionDesc S1
positionID 22 | positionDesc S2
positionID 55 | positionDesc S3