我正在使用以下方式从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);
?>
这段代码在重复使用时效果很好,但随着网站规模的扩大,我预计将会遇到问题,因为“topictype”选项的数量会增加(预计大约有30个类别)。我也不想为每个分类组的光盘制作单独的查询,因为随着类别的增加,最终会运行30个查询,希望能听到一些建议或替代方法 :) 谢谢。