[英]How can I print one thing when table1.id=table2.id, and another thing 'if else'?
I have a menu where some pages have sub-pages which open the drop-down menu. 我有一个菜单,其中某些页面包含可打开下拉菜单的子页面。
I have two SQL tables: 我有两个SQL表:
'pages' => page_id, page_name; 'subpages' => subpage_id, page_id, page_name;
Subpages get same page_id as the chosen parent page has, when I insert them via my form. 当我通过表单插入子页面时,它们与所选父页面具有相同的page_id。
The problem is, all menu elements show the drop-down arrow - even the ones without sub-pages. 问题是,所有菜单元素都显示下拉箭头-甚至没有子页面的菜单元素也是如此。
How can I print something only if the page has sub-pages? 仅当页面有子页面时,才可以打印内容吗? This is what i want:
这就是我要的:
if pages.page_id=subpages.page_id
print
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
<i class="fa fa-caret-down"></i>
</button>
else print
<a href="#"><?php echo $page['page_name']; ?></a>
index.php: index.php:
<?php foreach ($pages as $page) { ?>
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-container">
<?php foreach ($subpages as $subpage) {
if($subpage['page_id'] == $page['page_id']) {
?>
<a href="../subpage.php?id=<?php echo $subpage['subpage_id']; ?>">
<?php echo $subpage['subpage_name']; ?>
</a>
<?php } } ?>
</div>
<?php } ?>
Any help is appreciated! 任何帮助表示赞赏!
First of all I would change Subpage
class like this: 首先,我将像这样更改
Subpage
类:
class Subpage {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages");
$query->execute();
return $query->fetchAll();
}
// Static function: Fetch all subpages of single page
public static function fetch_all_page($page_id){
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages WHERE page_id = ?");
$query->bindValue(1, $page_id);
$query->execute();
return $query->fetchAll();
}
public function fetch_data($subpage_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM subpages WHERE subpage_id =
?");
$query->bindValue(1, $subpage_id);
$query->execute();
return $query->fetch();
}
}
And your index.php will become: 您的index.php将变为:
<?php foreach ($pages as $page) { ?>
<?php $subpages = Subpage::fetch_all_page($page['page_id']); ?>
<button class="dropdown-btn">
<?php echo $page['page_name']; ?>
<?php if (count($subpages)): ?>
<i class="fa fa-caret-down"></i>
<?php endif; ?>
</button>
<div class="dropdown-container">
<?php foreach ($subpages as $subpage): ?>
<a href="../subpage.php?id=<?php echo $subpage['subpage_id']; ?>">
<?php echo $subpage['subpage_name']; ?>
</a>
<?php endforeach; ?>
</div>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.