简体   繁体   中英

I want to display different content based on page_id, but on every page 'echo page_id' result is the same

I have a dynamic website with pages and their subpages. Pages get their structure from page.php and subpages from subpage.php file. My question is, how can I give different structure to subpages that have page_id=5?

Lets say, subpages have content such as:

<div class="a">
  <div class="b">
   <h1>some text</h1>
   <img src="">
  </div>
</div>

but if subpages parent-page id is 5, the content should be something like

<div class="c">
  <h3>some text</h3>
</div

I tried to echo page_id on subpages.. but for some reason, on every subpage, it echoes the latest inserted page_id..

My SQL tables:

table pages : page_id (AI, PK), page_name

table subpages : subpage_id (AI, PK), page_id, subpage_name

class.php file:

class Page {
    public function fetch_all(){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM pages");
        $query->execute();

        return $query->fetchAll();
    }    
    public function fetch_data($page_id) {
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM pages WHERE page_id = ?");
        $query->bindValue(1, $page_id);
        $query->execute();

        return $query->fetch();
    }

}
class Subpage {
    public function fetch_all(){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM subpages");
        $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();
    }
}

subpages.php file:

<?php
  include_once('class.php');
  $page = new Page;
  $subpage = new Subpage;

  $pages = $page->fetch_all();
  $subpages = $subpage->fetch_all();
?>

<!DOCTYPE html>
 <html>
  <head></head>
<body>

<!-- SOME EXAMPLE -->

  <?php if ($subpage['page_id'] != 5) { 
        echo 'id is not 5';
      } else {
        echo 'id is 5';
  } ?>

</body>
 </html>

I tried to echo page_id on subpages.. but for some reason, on every subpage, it echoes the latest inserted page_id..

you need to filter also the pageId on your subpage query.

public function fetch_data($subpage_id, $page_id) {
    global $pdo;

    $query = $pdo->prepare("SELECT * FROM subpages WHERE subpage_id = ? and page_id=?");
    $query->bindValue(1, $subpage_id);
    $query->bindValue(2, (int)trim($page_id), PDO::PARAM_INT);
    $query->execute();

    return $query->fetch();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM