所以我想用代码点火器从数据库中加载图像。这是我的密码:
控制器: Pweb
public function display($id=FALSE)
{
if ($id==FALSE){
$data["home_post"] = $this->M_pweb->displays();
$this->load->view('header');
$this->load->view('upload', $data);
$this->load->view('footer');
}else{
$data["post"] = $this->M_pweb->displays($id);
$this->load->view('header');
$this->load->view('upload', $data);
$this->load->view('footer');
}
}
模型: M_pweb
public function displays($id=FALSE)
{
if ($id==FALSE){
return $this->db->get("post")->result_array();
}else{
$query = $this->db->get_where("post", array('id'=>$id));
return $query->row();
}
}
视图:上载
<ul class="collection">
<?php foreach ($home_post as $data ): ?>
<li class="collection-item avatar">
<img src="<?=site_url("upload/post/".$data["filename"]) ?>" class="circle">
<p class="title"><?= $data["name"];?></p>
<small><?= $data["description"];?></small>
<a href="<?= site_url("pweb/upload/".$data["id"]) ?>" class="secondary-content">
<i class="material-icons">visibility</i>
</a>
</li>
<?php endforeach ?>
</ul>
它会产生这样的错误:
遇到严重程度时出错:注意
消息:未定义变量: home_post
文件名:view/upad.php
线路号码: 47
回溯:
档案: C:\xampp\htdocs\pweb\application\views\upload.php行: 47
功能:_error_handler
档案: C:\xampp\htdocs\pweb\application\controllers\Pweb.php行: 117
功能:视图
和
遇到严重程度错误:警告
消息:为foreach()提供的无效参数
文件名:view/upad.php
线路号码: 47
回溯:
档案: C:\xampp\htdocs\pweb\application\views\upload.php行: 47
功能:_error_handler
档案: C:\xampp\htdocs\pweb\application\controllers\Pweb.php行: 117
功能:查看
发布于 2021-04-28 06:36:34
只需将控制器替换为以下代码:(在HTML代码中,foreach循环使用"home_post“变量,该变量在控制器的其他条件中没有定义。
控制器: Pweb
public function display($id=FALSE)
{
if ($id==FALSE){
$data["home_post"] = $this->M_pweb->displays();
$this->load->view('header');
$this->load->view('upload', $data);
$this->load->view('footer');
}else{
$data["home_post"] = $this->M_pweb->displays($id);
^
$this->load->view('header');
$this->load->view('upload', $data);
$this->load->view('footer');
}
}
https://stackoverflow.com/questions/67294054
复制相似问题