我想通过id在同一个<div>
组中显示数据。我该怎么做呢?请参考图片,因为我解释不好。
这是我尝试过的一些代码。我不知道该怎么做
<?php
$result = $mysqli->query("SELECT tblstudent.studentId,tblstudent.programme,tblstudent.cgpa,tblpersonalinfo.studentId,tblpersonalinfo.pImage,tblpersonalinfo.pImageType,tblpersonalinfo.pImageSize,tblpersonalinfo.pName,tblskill.skill FROM tblstudent INNER JOIN tblpersonalinfo ON tblstudent.studentId = tblpersonalinfo.studentId INNER JOIN tblskill ON tblpersonalinfo.studentId = tblskill.studentId ".$where_sql." ");
if ($result->num_rows != 0) {
echo "<table class='scroll' width='700' border='0' bgcolor='#FF00FF' align='left'>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)) {
// get data from db
echo "<input type='hidden' name='id' value='".$row[0]."'>";
echo "<tr>";
echo "<td>";
echo '<div class="title bg-primary" align="left" style="font-weight: bold;">' . ' ' . '<img src="uploads/' . $row['pImage'] .' " alt = "avatar" height="60" width="50" >' . ' ' . $row['pName'] . '</div>';
echo '<div align="left" class="sub">' . '<br>' . $row['programme']. '</div>';
if ($row[0] == $row[0] ) {
echo "<table class='scroll' width='700' border='1' bgcolor='#FF00FF' align='left'>";
echo "<tbody>";
echo "<tr>";
echo "<td>";
echo '<div align="left" class="sub">' . '<br>' . $row['skill']. '</div>';
echo "</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
}
}
?>
答案 0 :(得分:0)
您必须预处理查询结果。像这样:
$user = array();
while( $row = mysqli_fetch_array($result) )
{
if( !isset($user['id']) )
{
$user['id'] = $row['studentId'];
// Add here all the other fields you want
$user['prg'] = array();
}
$user['prg'][] = array( 'skill'=>$row['skill'], 'proficiency'=>$row['proficiency'] );
}
您可以输出表格:
echo '<table>';
(...)
foreach( $user['prg'] as $prg )
{
echo '<tr>';
echo "<td>{$prg[skill]}</td>";
echo '</tr>';
}
(...)
echo '</table>';
通过SQL查询,您可以获得多行,包含重复字段(用户ID等)。您只能在while
的第一个循环中添加这些字段(在$user[id]
已设置之后)。 skill
和proficiency
的值会在每个循环中添加到子数组$user[prg]
。因此,当您输出表时,您将处理单个数组而不是多行,并且您将循环限制为$user[prg]
子数组。
另一种方法是在MySQL查询中使用GROUP_CONCAT。
答案 1 :(得分:0)
这样做:
<?php
$result = $mysqli->query("SELECT tblstudent.studentId,tblstudent.programme,tblstudent.cgpa,tblpersonalinfo.studentId,tblpersonalinfo.pImage,tblpersonalinfo.pImageType,tblpersonalinfo.pImageSize,tblpersonalinfo.pName,tblskill.skill FROM tblstudent INNER JOIN tblpersonalinfo ON tblstudent.studentId = tblpersonalinfo.studentId INNER JOIN tblskill ON tblpersonalinfo.studentId = tblskill.studentId ".$where_sql." ");
if ($result->num_rows != 0) {
echo "<table class='scroll' width='700' border='0' bgcolor='#FF00FF' align='left'>";
echo "<tbody>";
$flag = true;
while($row = mysqli_fetch_array($result)) {
// get data from db
if($flag){
echo "<input type='hidden' name='id' value='".$row[0]."'>";
echo "<tr>";
echo "<td>";
echo '<div class="title bg-primary" align="left" style="font-weight: bold;">' . ' ' . '<img src="uploads/' . $row['pImage'] .' " alt = "avatar" height="60" width="50" >' . ' ' . $row['pName'] . '</div>';
echo '<div align="left" class="sub">' . '<br>' . $row['programme']. '</div>';
echo "<table class='scroll' width='700' border='1' bgcolor='#FF00FF' align='left'>";
echo "<tbody>";
$flag=false;
}
echo "<tr>";
echo "<td>";
echo '<div align="left" class="sub">' . '<br>' . $row['skill']. '</div>';
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
}
?>
您应该确保<tr></tr>
循环中只重复while
个代码。休息一切都应该只显示一次。
答案 2 :(得分:0)
解决方案:
if ($result->num_rows != 0) {
echo "<table class='scroll' width='700' border='0' bgcolor='#FF00FF' align='left'>";
echo "<tbody>";
$buffer = '';
while($row = mysqli_fetch_array($result)) {
// get data from db
echo "<input type='hidden' name='id' value='".$row[0]."'>";
echo "<tr>";
echo "<td>";
if($buffer != $row[0]){
echo '<div class="title bg-primary" align="left" style="font-weight: bold;">' . ' ' . '<img src="uploads/' . $row['pImage'] .' " alt = "avatar" height="60" width="50" >' . ' ' . $row['pName'] . '</div>';
$buffer = $row[0];
}
echo '<div align="left" class="sub">' . '<br>' . $row['programme']. '</div>';
if ($row[0] == $row[0] ) {
echo "<table class='scroll' width='700' border='1' bgcolor='#FF00FF' align='left'>";
echo "<tbody>";
echo "<tr>";
echo "<td>";
echo '<div align="left" class="sub">' . '<br>' . $row['skill']. '</div>';
echo "</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
}
}