我正在使用以下内容从 mysql 数据库中获取我的大型结果集:
$discresult = 'SELECT t.id, t.subject, t.topicimage, t.topictype, c.user_id, c.disc_id FROM topics AS t LEFT JOIN collections AS c ON t.id=c.disc_id WHERE c.user_id='.$user_id;
$userdiscs = $db->query($discresult) or error('Error.', __FILE__, __LINE__, $db->error());
这将返回用户拥有的所有项目的列表。然后,我需要根据“topictype”列的值对这些项目进行分类,我目前正在使用:
<h2>Category 1</h2>
<?php
while ($cur_img = mysql_fetch_array($userdiscs)) {
if ($cur_img['topictype']=="cat-1") {
if ($cur_img['topicimage']!="") {
echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"".$cur_img['topicimage']."\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
} else {
echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"img/no-disc-art.jpg\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
}
}
}
mysql_data_seek($userdiscs, 0);
?>
<h2>Category 2</h2>
<?php
while ($cur_img = mysql_fetch_array($userdiscs)) {
if ($cur_img['topictype']=="cat-2") {
if ($cur_img['topicimage']!="") {
echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"".$cur_img['topicimage']."\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
} else {
echo "<div><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\"><img src=\"img/no-disc-art.jpg\" style=\"width:60px; height: 60px\" /></a><br /><a href=\"viewtopic.php?id=".$cur_img['id']."\" title=\"".$cur_img['subject']."\">".$cur_img['subject']."</a></div>";
}
}
}
mysql_data_seek($userdiscs, 0);
?>
当我冲洗并重复代码时,这工作得很好,但随着网站的增长,我预计随着“主题类型”选项的数量增加(预计大约 30 个类别)我会遇到问题。我也不想对每个分类的光盘组进行单独的查询,因为随着类别的增加,Id 最终会运行 30 个查询,所以希望听到一些建议或替代方法:)
谢谢