简体   繁体   English

我想根据 page_id 显示不同的内容,但是在每个页面上'echo page_id'结果都是一样的

[英]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.页面的结构来自 page.php,子页面来自 subpage.php 文件。 My question is, how can I give different structure to subpages that have page_id=5?我的问题是,如何为 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但如果子页面父页面 id 为 5,则内容应该类似于

<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..我试图在子页面上回显 page_id.. 但由于某种原因,在每个子页面上,它都会回显最新插入的 page_id..

My SQL tables:我的 SQL 表:

table pages : page_id (AI, PK), page_name table pagespage_id (AI, PK), page_name

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

class.php file: class.php 文件:

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 文件:

<?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..我试图在子页面上回显 page_id.. 但由于某种原因,在每个子页面上,它都会回显最新插入的 page_id..

you need to filter also the pageId on your subpage query.您还需要过滤subpage查询中的pageId

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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