我想从数据库中获取两个值“电影总数”和“电影细节”。 对于电影总数,我将返回php错误“undefined_index:num_films”。 请帮帮我。
Sakila_control
<?php
class Sakila_control extends CI_Controller
{
function display($offset=0)
{
$limit = 20;
$this->load->model('films_model');
$result = $this->films_model->search($limit,$offset);
$data['films']=$result['rows'];
$data['num_results']=$result['num_films'];
$this->load->view('films_view',$data);
}
}
Films_model
<?php
class Films_model extends CI_Model
{
function search($limit,$offset)
{
$qu = $this->
db->select('FID,title,description,category,price,length,rating,actors')
->from('film_list')
->limit($limit,$offset);
//rows comes from controller
$ret['rows']= $qu->get()->result();
return $ret;
$q = $this->db->select(`COUNT(*) as count`, FALSE)
->from('film_list');
//num_rows comes from controller
$tmp= $q->get()->result();
$ret['num_films']=$tmp[0]->count;
return $ret;
}
}
Films_view
<body>
<table>
<div>
Found <?php echo $num_results;?> Films
</div>
<thead>
<th>ID</th>
<th>Title</th>
<th>description</th>
<th>category</th>
<th>price</th>
<th>length</th>
</thead>
<tbody>
<?php
foreach($films as $film):?>
<tr>
<td><?php echo $film->FID;?> </td>
<td><?php echo $film->title;?></td>
<!-- <td><?php echo $film->description;?></td> -->
<td><?php echo $film->category;?></td>
<td><?php echo $film->price;?></td>
<td><?php echo $film->length;?></td>
<td><?php echo $film->rating;?></td>
<!-- <td><?php echo $film->actors;?></td> -->
</tr>
<?php endforeach;?>
</tbody>
</table>
</body>
</html>
答案 0 :(得分:0)
根据您的代码,您将在第一次查询后返回值。所以你的函数没有返回num_films
。
class Films_model extends CI_Model
{
function search($limit,$offset)
{
$qu = $this->
db->select('FID,title,description,category,price,length,rating,actors')
->from('film_list')
->limit($limit,$offset);
//rows comes from controller
$ret['rows']= $qu->get()->result();
// return $ret;
$q = $this->db->select('COUNT(*) as count`, FALSE)
->from('film_list');
//num_rows comes from controller
$tmp= $q->get()->result();
$ret['num_films']=$tmp[0]->count;
return $ret;
}
}
检查同一function
中的多个回复答案 1 :(得分:0)
首先你需要注释掉这个:
// return $ret;
然后,由于返回语句,你需要用你没有得到的反引号修复另一个错误......
PHP中的反引号将尝试从命令行运行代码。
改变这个:
$q = $this->db->select(`COUNT(*) as count`, FALSE)
对此:
$q = $this->db->select('COUNT(*) as count', FALSE)
最终结果:
<?php
class Films_model extends CI_Model
{
function search($limit,$offset)
{
$qu = $this->
db->select('FID,title,description,category,price,length,rating,actors')
->from('film_list')
->limit($limit,$offset);
//rows comes from controller
$ret['rows']= $qu->get()->result();
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('film_list');
//num_rows comes from controller
$tmp= $q->get()->result();
$ret['num_films']=$tmp[0]->count;
return $ret;
}
}
在此处阅读有关反引号的更多信息:http://php.net/manual/en/language.operators.execution.php
执行操作员
PHP支持一个执行操作符:反引号(``)。请注意,这些不是单引号! PHP将尝试作为shell命令执行反引号的内容;将返回输出(即,它不会简单地转储到输出;它可以分配给变量)。使用反引号运算符与shell_exec()相同。
答案 2 :(得分:0)
这个怎么样?
在模型中
class Films_model extends CI_Model
{
function search($limit,$offset)
{
# Just pass all data to controller
$query = $this->db->query("SELECT * FROM film_list LIMIT $limit, $offset");
$result = $query->result_array(); # Changed
return $result;
}
}
在控制器中
class Sakila_control extends CI_Controller
{
function display()
{
$limit = 20;
$offset=0
$this->load->model('films_model');
$data['films'] = $this->films_model->search($limit,$offset);
$count = count($data['films']); # get count on here
$data['num_results'] = $count;
$this->load->view('films_view',$data);
}
}
在视图中
一切都很好,由你决定