我在CodeIgniter的“视图”中出现错误 看起来像这样的错误
遇到PHP错误严重性:警告消息:无效 为foreach()文件名提供的参数: public / search_query_result.php行号:17
我的 search_query_result.php
中的代码<div class="container">
<h1>All Articles</h1>
<hr>
<table class="table">
<thead>
<tr>
<th>Sr.No</th>
<th>Article Title</th>
<th>Created On</th>
</tr>
<tbody>
<?php if(count($articles)){
$count=$this->uri->segment(3,0);
foreach($articles as $article){
?>
<tr>
<td><?=++$count;?></td>
<td><?= $article->title?></td>
<td><?= "Date"?></td>
</tr>
<?php }?>
users.php 中的代码。这是“控制器”文件:
function search_articles(){
$this->load->library('form_validation');
$this->form_validation->set_rules('query','Query','required');
if(!$this->form_validation->run())
$this->index();
$query=$this->input->post('query');
return redirect("users/search_result/$query");}
unction search_result($query){
$this->load->helper('form');
$this->load->model('articlesmodel');
$this->load->library('pagination');
$config=[
'base_url' => base_url("users/search_result/$query"),
'per_page' => 3,
'total_rows' =>$this->articlesmodel->count_search_result($query),
'full_tag_open' => '<ul class="pagination"> <a >',
'full_tag_close' => '</a></ul>',
'next_tag_open' => '<li class="page-item"><a>',
'next_tag_close' => '</a></li>',
'prev_tag_open' => '<li class="page-item"><a>',
'prev_tag_close' => '</a></li>',
'num_tag_open' => '<li class="page-item"><a>',
'num_tag_close' => '</a></li>',
'cur_tag_open' => '<li class="active"><a>',
'cur_tag_close' => '</a></li>',
];
$articles=$this->articlesmodel->count_search_result($query,$config['per_page'],$this->uri->segment(4));
$this->load->view('public/search_query_result',compact('articles'));
}
来自 articlesmodel.php 的代码。这是模型文件
function num_rows(){
$user_id=$this->session->userdata('user_id');
$query=$this->db
->select(['title','id'])
->from('articles')
->where('user_id',$user_id)
-> get();
// print_r($query->result());
return $query->num_rows();
}
function search_articles( $query){
$q= $this->db
->from('articles')
->like('title',$query)
->limit($limit,$offset)
->get();
return $q->result();
}
function count_search_result($query){
$q= $this->db->from('articles')
->like('title',$query)
->get();
return $q->num_rows();
}
答案 0 :(得分:1)
查看您的代码,似乎count_search_result函数将返回num_rows并存储在$ articles变量中,并且您试图遍历该变量以回显$ article-> title,该标题不可用。
您必须在控制器行下更新
$articles=$this->articlesmodel->count_search_result($query,$config['per_page'],$this->uri->segment(4));
到
$articles=$this->articlesmodel->search_articles($query);
这将为您提供所需的文章表中的列值。
答案 1 :(得分:0)
在(users.php)
我在控制器内部传递了错误的函数名。区别降低了……。
之前
$articles=$this->articlesmodel->count_search_result($query,$config['per_page'],$this->uri->segment(4));
之后
$articles=$this->articlesmodel->search_articles($query,$config['per_page'],$this->uri->segment(4));
这是区别
$articles=$this->articlesmodel->count_search_result
更改为$articles=$this->articlesmodel->search_articles