Controller:
public function latestnews()
{
$data['news'] = $this->New_model->getById($id);
$this->load->view('news',$data);
}
Model:
public function getById($id)
{
return $this->db->get_where($this->_table, ["new_id" => $id])->row();
}
View:
<?php
if (isset($news) and $news) {
foreach($news as $new) {
?>
<div class="col-sm-12">
<div class="section">
<img src="<?php echo site_url('uploads/'.$new->image); ?>" />
</div>
<p><?php echo $new->description;?> </p>
</div>
<?php
}
}
?>
如何以及在何处定义变量 id?
单击动态图像时,应在包含详细信息的另一个页面中打开它,但显示未定义的变量:id
在latestnews()
函数中,您没有定义$id
。
尝试使用正确的$id
参数调用latestnews()
函数。
你可以这样写函数,
function latestnews($id)
{
控制器:
function single_news($id) { // This $id variable comes from URL Ex: example.com/single_news/15. So $id = 15
$this->data['news_data'] = $this->new_model->getNewsByID($id);
$this->load->view('single-news', $this->data); // Create New View file named single-news.php
}
型:
function getNewsByID($id) {
$this->db->where('id', $id);
$q = $this->db->get('news');
if($q->num_rows() > 0) {
return $q->row();
}
return false;
}
查看:(单个新闻页面(
<div>
<img src="<?php echo site_url('uploads/'.$news_data->image); ?>" />
</div>