为什么无法从CodeIgniter中的数据库获得预期结果? [英] Why cant get expected result from database in CodeIgniter?
问题描述
我的模型如下
$values = array_count_values($books);
arsort($values);
$newArray = array_keys($values);
$views_books_array = array_slice($newArray, 0, 5);
$result = $this->db->where_in('book_id',$views_books_array)->get('books');
return $result->result_array();
新数组将如下所示
数组([0] => 37 [1] => 28 [2] => 31 [3] => 30 [4] => 38)
Array ( [0] => 37 [1] => 28 [2] => 31 [3] => 30 [4] => 38 )
但是当我获取了检索数据,它将从不是我给的随机数据中获取数据
but when i get retrieve data it will get data from randomly not what i have given
im将book_id传递给模型和控制器
,如下所示,
im passing book_id to model and controller my controller like below,
$ data ['result'] = $ this-> Book_Model-> get_viewed_books($ book_id);
$data['result'] = $this->Book_Model->get_viewed_books($book_id);
如果我print_r结果将类似于
if i print_r result will be like
数组([0] => 28 [1] => 30 [2] => 31 [3] => 37 [4] => 38)
Array ( [0] => 28 [1] => 30 [2] => 31 [3] => 37 [4] => 38 )
那样,但是我要根据newArray的数据我做错了什么
like that but i want data according to newArray what did i do wrong
请帮助我
推荐答案
以下是直接从手册中摘录并适合您的情况的示例:
Here's an example taken straight from the manual and adapted to your case:
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
$arrlength = count($numbers);
for($x = 0; $x < $arrlength; $x++) {
echo $numbers[$x];
echo "<br>";
}
?>
您还可以通过查询定义此示例。
You can also define this with your query Example are given below :
$this->db->order_by('column_name', 'ASC');
这篇关于为什么无法从CodeIgniter中的数据库获得预期结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!