在另一个 while 循环中使用一个 while 循环的结果

在另一个 while 循环中使用一个 while 循环的结果

问题描述:

我在尝试在 while 循环中创建 while 循环时遇到了大麻烦.第一个while循环应该输出类别.第二个 while 循环应该输出与类别关联的链接.链接信息在链接表中,类别信息在类别表中.我正在尝试对 category.ID 上的 links.catID 进行 JOIN.

I'm having big trouble trying to create a while loop within a while loop. The first while loop is supposed to output categories. The second while loop is supposed to output the links associated with the category. The link information is in a links table and the categories information in the category table. I'm trying to do a JOIN on links.catID on category.ID.

链接表:
id 链接 url catID 类型

类别表:
id 猫类型

links.catID 应该与 int 中的 category.id 相等.category.cat 然后将输出类别的名称.

links.catID is supposed to equal category.id in ints. category.cat would then output the name of the category.

我的代码是:

require 'db/connect.php';
$sql = "SELECT * FROM category";
$results = $db->query($sql);
echo "<p class='post-footer align-left'><span class='pageName'><span 
class='style3'>Links</span></span></p>";
    if($results->num_rows){
        while($row = $results->fetch_object()){
        echo "<table width='507' border='1'>";
        echo " <p class='post-footer align-left'><span class='pageName'><span class='style3'>{$row->cat}</span></span></p>";
        $sql1 = "SELECT links.url, links.links, links.catID FROM links INNER JOIN category on category.id WHERE category.id = links.catID";
        $results1 = $db->query($sql1);
            if ($results1->num_rows > 0) {
                while($row1 = $results1->fetch_object()){
                    echo "<th><span class='pageName'><span class='style3'>{$row1->links}</span></span></th>";
                }
            }
        }
    }

请善待我,我的经验很少,这很可能在上面显示.我已经被困在这个问题上一段时间了,只是想不通.我真的很感激有人的帮助.

Please be kind to me, I have very little experience and that very likely shows in the above. I have been stuck on this for a while now and just can't figure it out. I would really appreciate someone's help.

我想我已经在上面正确解释了:)

I think I have it explained properly above :)

不要担心 PHP 的 CSS.CSS 很容易修复.只需要列出正确类别的正确链接即可.

Don't worry about the CSS of the PHP one. The CSS is easy to fix. Just need to only list the right links with the right categories.

作为我第一个回答的后续,下面的示例展示了如何使用单个查询来完成此操作.恕我直言,您应该始终选择这种方法.如果您只有少数几个类别要钻取,如果您坚持使用查询嵌套在循环内的方法,则性能消耗可能不会太大.但如果您有大量类别,下面的方法应该会更有效:

As a follow-up to my first answer, the example below shows how you can do this with a single query. IMHO, you should always opt for this approach. If you only have a handful of categories to drill into, it may not be much of a performance drain if you stick with the query-nested-inside-a-loop approach. But if you have a sizable number of categories, the approach below should be significantly more efficient:

require 'db/connect.php';
$sql = "
    SELECT
        category.id
        , links.links
    FROM
        category
    LEFT JOIN
        links ON links.catID = category.id
    ORDER BY
        category.id ASC
        -- this ORDER BY is critical, it ensures that all of the
        -- records will be lumped by category
";
// (yes, this query will create multiple rows for every category, but that's OK,
// we're going to deal with that in the while() loop
$results = $db->query($sql);
echo "<p class='post-footer align-left'><span class='pageName'><span class='style3'>Links</span></span></p>";
if($results->num_rows){
    $previousId = '';
    while($row = $results->fetch_object()){
        $currentId = $row->id;
        if ($currentId !== $previousId) {
            // we only show the wrapper <table>, and the category header, if we've encountered a new
            // batch of rows which signifies that a new category is being processed
            echo "<table width='507' border='1'>";
            echo " <p class='post-footer align-left'><span class='pageName'><span class='style3'>{$row->cat}</span></span></p>";
        }
        echo "<th><span class='pageName'><span class='style3'>{$row->links}</span></span></th>";
        if ($currentId !== $previousId and $previousId !== '') {
            echo "</table>";  // this wasn't in your original code, but I assume it should be here
        }
        $previousId = $currentId;
    }
}
echo "</table>";