我需要隐藏所有没有链接的类别。基本上,我拥有的是所有可能类别的表格和每个类别的唯一 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;
}
谢谢你的时间。
此单个查询连接两个表,因此它仅返回lcategories
中具有匹配项的行 Files
。它按 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";
使用 if 条件和继续跳过以使您的循环跳到下一次。
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