我正在检索表中的所有记录以在php页面上回显它们,问题是表中有3列重复的行具有相似值,有人可以帮我解决这段代码,我需要分组什么?该组只检索了一行,请回复。分组的字段将重复,添加了新记录的字段为secondarySubject和secondaryGrade
我得到的结果是:
---------------------------------
2012-2013 School ABC A-Level
in science A
2012-2013 School ABC A-Leve
in maths B
2012-2013 School ABC A-Level
in Eng C
-----------------------------------
我想要的是
----------------
2012-2013 School ABC A-Level
in science A
in maths B
in Eng C
-----------------------
$result2 = $db->query('
SELECT *
FROM secondaryEducation
WHERE userID = "'.$graduateID.'"
ORDER BY secondaryFinishDate DESC
');
$html .= '<h2>Secondary Education: '.$option.'</h2>';
while($row = mysql_fetch_assoc($result2))
{
$startYear = formatDate($row['secondaryStartDate'], 'Y');
$finishYear = formatDate($row['secondaryFinishDate'], 'Y');
if (!empty($row['secondaryGrade']))
$secondaryGrade = getSecondaryGradeName($row['secondaryGrade']);
else
$secondaryGrade = $row['secondaryGradeCustom'];
if (!empty($row['secondarySubject']))
$secondarySubject = getSecondarySubjectName($row['secondarySubject']);
else
$secondarySubject = $row['secondarySubjectCustom'];
$score = $row['score'];
$secondaryLevel = getSecondaryLevelName($row['secondaryLevel']);
$levelID = getSecondaryLevelID($row['secondaryLevel']);
if($levelID!=='7')
{
$html .= '
<div class="secondaryListing">
<div><strong>'.$startYear.' - '.$finishYear.' '.stripslashes($row['secondarySchool']).'</strong></div>
<div>'.$secondaryLevel.' in '.$secondarySubject.' - '.stripSlashes($secondaryGrade).'</div>
</div><!-- End education listing -->';
}
答案 0 :(得分:0)
在while循环之前,创建一个数组:
$schools = array();
while($row = mysql_fetch_assoc($result2))
...
不是附加到$html
,而是将字符串添加到$schools
关联数组中:
if($levelID!=='7')
{
$schools[$startYear.' - '.$finishYear.' '.stripslashes($row['secondarySchool']) . ' ' . $secondaryLevel][]=' in '.$secondarySubject.' - '.stripSlashes($secondaryGrade);
}
最后,最后,迭代$schools
关联数组并将字符串追加到$html
:
foreach($schools as $group => $entries)
{
$html .= '<div class="secondaryListing"><div><strong>'.$group.'</strong></div>';
foreach($entries as $entry) {
$html .= '<div>'.$entry.'</div>';
}
$html .='</div><!-- End education listing -->';
}