问题检索PHP中HTML视图的帖子列表
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方法或更清洁的编码方式将宣布$数据数组为属性:
在您的施工方法中,您现在可以启动这样的数组:(
实际值取决于您的数据结构,传递默认值不是一个 主意
。
坏 您可以在其中解决。从方法声明中删除参数:
并将其修改为这样的for-loop:
但要抬头:这样,您需要确保正确填充并清理$ Data-Property。现在在类实例中有点持久。
A workaround and maybe a cleaner way of coding would be declaring the $data array as a property:
Within your construction method you may now initiate the array like that:
(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:
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:
And modify your for-each-loop like that:
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.
我不习惯在PHP中进行编码的方式,但是对我来说,您的功能名称索引是您无法获得所需的原因。尝试将
返回$ 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: