简体   繁体   English

当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印?

[英]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.

相关问题 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause MySQL使用table1.id REGEXP table2.id为同一组选择不同的行 - MySQL select distinct rows for same group using table1.id REGEXP table2.id 如何将一个表的ID值获取到另一个表 - How can i get one table id values to another table Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id - Laravel one page crud Select all from table2 where id = table1.id 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id 如何在另一个表的一行中传递一个表的ID? - How I pass the id from one table in a row in another table? 如何检查一个表到另一个表的ID? - How to check id of one table to another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM