使用 php while 对大型 mysql 结果进行分类?

发布于 2024-08-22 18:30:44 字数 2618 浏览 11 评论 0原文

我使用以下命令从 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 个查询,所以希望听到一些建议或替代方法:)

谢谢

I'm using the following to grab my large result set from a mysql db:

$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());

This returns a list of all items that the user owns. I'm then needing to categorise these items based on the value of the "topictype" column, which Im currently doing by using:

    <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);
    ?>

This works fine when I rinse and repeat the code, but as the site grows I expect I'll run into problems as the number of "topictype" options increases (expecting around 30 categories). I dont want to have to make seperate queries for each categorised group of discs either, as Id eventually have 30 queries being run as categories increase, so hoping to hear some suggestions or alternative approaches :)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉黛浅 2024-08-29 18:30:44

您可以尝试添加“ ORDER BY t.topictype”,然后在类别更改时发出标头

$category = "";

while ($cur_img = mysql_fetch_array($userdiscs)) 
{
    if ($cur_img['topictype'] != $category )
    {
        $category = $cur_img['topictype'];
        echo '<h2>', $category, '</h2>';
    }

    .... rest of output here
}

you might try adding " ORDER BY t.topictype" and then emitting the header when the category changes

$category = "";

while ($cur_img = mysql_fetch_array($userdiscs)) 
{
    if ($cur_img['topictype'] != $category )
    {
        $category = $cur_img['topictype'];
        echo '<h2>', $category, '</h2>';
    }

    .... rest of output here
}
酒废 2024-08-29 18:30:44

您只能获取一次,按类别分组,然后显示每个类别:

$userdiscs = mysql_fetch_array($userdiscs);
$categories = Array ();
foreach ($userdiscs as $cur_img) {
  $category = $cur_img['topictype'];
  if (isset ($categories[$category])) $categories[$category] = Array();
  $categories[$category][] = $cur_img;
}
foreach ($categories as $category => $imgs) {
  echo '<h2>', $category, '</h2>';
  foreach ($imgs as $cur_img) {  
    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>";
    }
  }
}

编辑:
如果 $cur_img['topictype'] 与您的类别 h2 标题不匹配,您可以使用如下所示的“哈希数组”:

$categories_names = Array (
  'cat-1' => 'Category 1',
  'cat-2' => 'Category 2',
  'cat-3' => 'Category 3'
)

并替换 echo '

', $category , '

'; with echo '

', $categories_names[$category], '

';

you can fetch that only once, group by categories and then display each one:

$userdiscs = mysql_fetch_array($userdiscs);
$categories = Array ();
foreach ($userdiscs as $cur_img) {
  $category = $cur_img['topictype'];
  if (isset ($categories[$category])) $categories[$category] = Array();
  $categories[$category][] = $cur_img;
}
foreach ($categories as $category => $imgs) {
  echo '<h2>', $category, '</h2>';
  foreach ($imgs as $cur_img) {  
    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>";
    }
  }
}

edit:
if $cur_img['topictype'] does not match your category h2 title, you can have a "hash-array" like this:

$categories_names = Array (
  'cat-1' => 'Category 1',
  'cat-2' => 'Category 2',
  'cat-3' => 'Category 3'
)

and replace echo '<h2>', $category, '</h2>'; with echo '<h2>', $categories_names[$category], '</h2>';

凉风有信 2024-08-29 18:30:44

响应 dar7yls 建议所使用的代码如下所示,每个类别仅显示一张光盘:

$discresult = 'SELECT t.topictype, t.id, t.subject, t.topicimage, 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.' ORDER BY t.topictype';
$userdiscs = $db->query($discresult);

$category = "";

while ($cur_img = mysql_fetch_array($userdiscs)) {
    if ($cur_img['topictype'] != $category ) {
    $category = $cur_img['topictype'];
    echo '<h2>', $category, '</h2>';

    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>";
    }
}
}

Code used in response to dar7yls suggestion is as shown below, and displaying only one disc per category:

$discresult = 'SELECT t.topictype, t.id, t.subject, t.topicimage, 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.' ORDER BY t.topictype';
$userdiscs = $db->query($discresult);

$category = "";

while ($cur_img = mysql_fetch_array($userdiscs)) {
    if ($cur_img['topictype'] != $category ) {
    $category = $cur_img['topictype'];
    echo '<h2>', $category, '</h2>';

    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>";
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文