Why cant get expected result from database in CodeIgniter?

被刻印的时光 ゝ 提交于 2019-12-25 00:25:22

问题


My model like below

            $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();

new array will be like below

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 passing book_id to model and controller my controller like below,

$data['result'] = $this->Book_Model->get_viewed_books($book_id);

if i print_r result will be like

Array ( [0] => 28 [1] => 30 [2] => 31 [3] => 37 [4] => 38 )

like that but i want data according to newArray what did i do wrong

please help me with this anything would appreciated


回答1:


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');



回答2:


I don't really anderstand what you want to do but if you want ordered result you can use this method:

$this->db->order_by('column', 'order');

The second parameter can be: 'ASC' or 'DESC'. Check the codeigniter manual.



来源:https://stackoverflow.com/questions/53183805/why-cant-get-expected-result-from-database-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!