0

我需要隐藏所有没有链接的类别。基本上,我所拥有的是所有可能类别的表和每个类别的唯一 ID,第二个表包含所有具有分配给每个小部件的父页面 ID 的小部件。父页面 ID 是相关类别项目的 ID(如果您愿意,请命名)。

现在我的问题是,使用您可以在下面看到的脚本,一切正常,除了一件事,所有类别项目都在显示,即使它们没有链接,我明白原因,但无法弄清楚整理出来。

请帮忙

    $category_topic_query = 'SELECT * FROM lcategories ORDER BY ID asc';
    $resc = $db->prepare($category_topic_query);
    $resc->execute();
    $template_link_query = "SELECT parentpageID, ImagePath, referring_url, templateTitle FROM Files WHERE parentpageID = :id AND pageID = '0'";
    $link_res = $db->prepare($template_link_query);

while ($category_topic = $resc -> fetch()){
    $category_topic_ID = $category_topic['ID'];
    $category_topic_name = str_replace("&", "&", $category_topic['category_name']);
    $category_topic_url = DST.$category_topic['category_folder'].DS.$category_topic['category_page'];
    $link_res->execute(array(':id' => $category_topic_ID));


print<<<END
<h3><a href="$category_topic_url">$category_topic_name</a></h3>
<ul class="arrow">

END;

while ($t_links = $link_res -> fetch()){
    $templateID = $t_link['parentpageID'];
    $links_array = '<li><a href="'.DST.$t_links['ImagePath'].DS.$t_links['referring_url'].'">'.$t_links['templateTitle'].'</a></li>';

print<<<END
$links_array

END;
}
print<<<END
</ul>

END;
}

谢谢你的时间。

4

2 回答 2

1

这个单一的查询连接两个表,所以它只返回lcategoriesFiles. 它的排序方式parentpageID是 ,与类别 ID 相同,因此同一类别中的所有行都会在结果中一起出现。然后 fetch 循环会注意到这个 ID 何时发生变化,并在那时打印类别标题。

$query = "SELECT l.category_name, l.category_folder, l.category_page,
                 f.parentpageID, f.ImagePath, f.referring_url, f.templateTitle
          FROM lcategories l
          INNER JOIN Files f ON f.parentpageID = l.ID
          WHERE f.pageID = '0'
          ORDER BY f.parentpageID";
$stmt = $db->prepare($query);
$stmt->execute();
$last_topic = null;
$first_row = true;
while ($row = $stmt->fetch()) {
    $category_topic_ID = $row['parentpageID'];
    if ($category_topic_ID !== $last_topic) {
        $category_topic_name = htmlentities($row['category_name']);
        $category_topic_url = DST.$row['category_folder'].DS.$row['category_page'];
        if (!$first_row) {
            print "</ul>\n;";
            $first_row = false;
        }
        print<<<END
<h3><a href="$category_topic_url">$category_topic_name</a></h3>
<ul class="arrow">

END;
        $last_topic = $category_topic_ID;
    }
    $links_array = '<li><a href="'.DST.$row['ImagePath'].DS.$row['referring_url'].'">'.$row['templateTitle'].'</a></li>';

    print<<<END
    $links_array

    END;
}
print "</ul>\n";
于 2013-09-14T03:02:04.023 回答
0

使用 if 条件和continue to skip 让您的循环跳到下一次。

while ($t_links = $link_res -> fetch()){
    $templateID = $t_link['parentpageID'];
    $links_array = '<li><a href="'.DST.$t_links['ImagePath'].DS.$t_links['referring_url'].'">'.$t_links['templateTitle'].'</a></li>';

// no like, so skip.
if (''==DST.$t_links['ImagePath'].DS.$t_links['referring_url']) continue;
print<<<END
于 2013-09-14T00:41:04.697 回答