问题检索PHP中HTML视图的帖子列表

发布于 2025-01-26 14:47:30 字数 2115 浏览 2 评论 0原文

我正在尝试使用PHP构建一个博客网站,其中所有帖子都可以公开可用,但是某种程度上,我遇到的问题要在页面上实际显示这些帖子。

查询的代码是:

<?php
    class Blog {
        private $db;
        
        public function __construct(){
            $this->db = new Database;
        }

        public function getBlogs(){
            $this->db->query('SELECT * FROM posts');
            $results = $this->db->resultSet();

            return $results;
        }

    }

生成视图的代码是:

public function __construct(){
           
            $this->blogModel = $this->model('Blog');
        }


        public function index(){
            // Get posts

            $posts = $this->blogModel->getPosts();

            $data = [
                'posts' => $posts
            ];

            $this->view('pages/blog', $data);
        }

视图的实际HTML看起来像这样:

<?php require APPROOT . '/views/inc/header.php'; ?>
    <div class="row mb-3">
        <div class="col-md-6">    
            <h1>Artículos</h1>
        </div>
    </div>
    <?php foreach($data['posts'] as $post) : ?>
        <div class="card card-body mb-3 mt-3">
            <h4 class="card-title"><?php echo $post->title; ?></h4>
            <a href="<?php echo URLROOT; ?>/posts/show/<?php echo $post->postId; ?>" class="btn button-custom-orange">More</a>
        </div>
    <?php endforeach; ?>

<?php require APPROOT . '/views/inc/footer.php'; ?>

但是,当我实际尝试转到页面/博客时,我会收到通知:未定义的索引: c:\ xampp \ htdocs \ edutechne \ app \ app \ view \ peg.pages \ blog.php中的帖子,这意味着数组显然没有传递给视图。

知道我的代码可能出了什么问题吗?

编辑:有人问我包括视图的代码,所以这里是:

public function view($view, $data = []){
         // Check for view file
         if(file_exists('../app/views/' . $view . '.php')){
            require_once '../app/views/' . $view . '.php';
         } else {
             // View does not exist
             die('View does not exist');
         }

     }

I'm trying to use php build a blog website where all the posts are publicly available, but somehow I'm having issues getting the posts to actually display on the page.

The code for the query is:

<?php
    class Blog {
        private $db;
        
        public function __construct(){
            $this->db = new Database;
        }

        public function getBlogs(){
            $this->db->query('SELECT * FROM posts');
            $results = $this->db->resultSet();

            return $results;
        }

    }

The code for generating the view is:

public function __construct(){
           
            $this->blogModel = $this->model('Blog');
        }


        public function index(){
            // Get posts

            $posts = $this->blogModel->getPosts();

            $data = [
                'posts' => $posts
            ];

            $this->view('pages/blog', $data);
        }

And the actual HTML for the view looks like this:

<?php require APPROOT . '/views/inc/header.php'; ?>
    <div class="row mb-3">
        <div class="col-md-6">    
            <h1>Artículos</h1>
        </div>
    </div>
    <?php foreach($data['posts'] as $post) : ?>
        <div class="card card-body mb-3 mt-3">
            <h4 class="card-title"><?php echo $post->title; ?></h4>
            <a href="<?php echo URLROOT; ?>/posts/show/<?php echo $post->postId; ?>" class="btn button-custom-orange">More</a>
        </div>
    <?php endforeach; ?>

<?php require APPROOT . '/views/inc/footer.php'; ?>

However, when I actually try to go to pages/blog, I get a Notice: Undefined index: posts in C:\xampp\htdocs\edutechne\app\views\pages\blog.php on line 7, meaning that the array is apparently not being passed to the view.

Any idea what might be wrong with my code?

EDIT: Someone asked that I include the code for the view so here it is:

public function view($view, $data = []){
         // Check for view file
         if(file_exists('../app/views/' . $view . '.php')){
            require_once '../app/views/' . $view . '.php';
         } else {
             // View does not exist
             die('View does not exist');
         }

     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2025-02-02 14:47:30

解决方法或更清洁的编码方式将宣布$数据数组为属性:

    class Blog {
        private $db;
        private $data;
        ...

在您的施工方法中,您现在可以启动这样的数组:(

    public function __construct(){
        $this->db = new Database;
        $this->data = ['posts' => ['no posts']]
    }

实际值取决于您的数据结构,传递默认值不是一个 主意

    public function index(){
        ...
        $this->data['posts'] = $posts
        ...

坏 您可以在其中解决。从方法声明中删除参数:

public function view($view){
   ...

并将其修改为这样的for-loop:

<?php foreach($this->data['posts'] as $post) : ?>

但要抬头:这样,您需要确保正确填充并清理$ Data-Property。现在在类实例中有点持久。

A workaround and maybe a cleaner way of coding would be declaring the $data array as a property:

    class Blog {
        private $db;
        private $data;
        ...

Within your construction method you may now initiate the array like that:

    public function __construct(){
        $this->db = new Database;
        $this->data = ['posts' => ['no posts']]
    }

(The actual value depends on your data structure, passing a default value is not a bad idea. I just assumed an array holding one item 'no posts')

Within your index method you pass the results to the property:

    public function index(){
        ...
        $this->data['posts'] = $posts
        ...

Now you do not need to pass the $data to the method $Blog->view(), you can address it within. Remove the argument from the method declaration:

public function view($view){
   ...

And modify your for-each-loop like that:

<?php foreach($this->data['posts'] as $post) : ?>

But heads up: This way you need to make sure to properly populate and clean up the $data-property. It's now kind of persistant within the class instance.

还在原地等你 2025-02-02 14:47:30

我不习惯在PHP中进行编码的方式,但是对我来说,您的功能名称索引是您无法获得所需的原因。尝试将返回$ data将其添加到您的代码中,这样看起来像这样:

public function index(){     

            $posts = $this->blogModel->getPosts();

            $data = [
                'posts' => $posts
            ];

            $this->view('pages/blog', $data);
            return $data;
        }

I am not used to this way of coding in php, but to me it looks like your function named index is reason you can't get what you need. Try adding return $data to your code so it looks like this:

public function index(){     

            $posts = $this->blogModel->getPosts();

            $data = [
                'posts' => $posts
            ];

            $this->view('pages/blog', $data);
            return $data;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文