1

My model returns data as mysql objects:

return $q->result();

Now, I want to shuffle/randomize the data and send it to the view file. But when I use the shuffle($data) on the object, it doesn't work, gives out an error. I guess, it will only work if my model returns an array.

Is there any way I can shuffle/randomize the data without converting it to an array, or without making the model return an array.

2
  • 1
    It might be efficient to actually show the error? Commented Jun 15, 2011 at 5:57
  • Message: Invalid argument supplied for foreach() Message is displayed in the view, where I use the foreach to loop through the shuffled result. Commented Jun 15, 2011 at 6:03

4 Answers 4

4

Have you thought about add $this->db->order_by('id', 'random'); when generating the query? The first parameter is the field name and the second the order (possible values are "asc", "desc", and "random"). Have a look at the order_by function for more information.

EDIT:

Alternatively, you could use result_array() instead of results(). Here's an example:

// PHP5
$shuffled_result = $this->db->get('table')->result_array();
shuffle($shuffled_result);

// PHP4
$query = $this->db->get('table');
$shuffled_result = $query->result_array();
shuffle($shuffled_result);
Sign up to request clarification or add additional context in comments.

7 Comments

Think this is the same as mentioned by flykobe.
More or less. This is the CodeIgniter way of doing it while his is the MySQL way of doing it. When using CodeIgniter, you should try to avoid writing your own SQL and use the functions that CodeIgniter provides. My solution is preferable if you're using CodeIgniter.
I've updated my code above to include a version that uses shuffle instead of ordering in SQL.
Yes, thanks. Looks like I will have to do it this way rather than using $query->result(). But I prefer to return out the rows as echo $data->row_name as opposed to echo $data['row_name'].
@Damchey - If you add a $data = (object)$data;, it will convert the array into an object and you'll be able to use $data->row_name.
|
1

Objects do not have any explicit order of properties. You should be using an array if you require ordering.

1 Comment

@Damchey I generally use an array with associative array members myself, but an object for rows would be fine too.
0

Well make you model to return the mysql data as array (give me the code if you don't know how) and from then you can use to shuffle the array with Shuffle function

I don't think you can shuffle the data base it self with query or something

1 Comment

I can use $query->result_array() to return an array, but I have been using $query->result() to return so far, and I want to keep it consistent. So was wondering if there was any way to randomize the objects. Now I think I should convert all my models to return array instead of objects.
0

In mysql, you can order rows with order by rand(), like this:

select product_id  from product order by rand() limit 10;

so, you can get appropriate number of rows you want.

EDIT:

@Damchey you're right. If the table is bigger, than the rand() function will be slow. But every time, how many rows you select? So, Maybe you can get a random offset in php, and sql will like this:

select product_id  from product offset $random_offset limit 10

Although, the order of product_id is same, but offset is different, so if your table is big, than everytime you can get different rows. And make sure, your offset if not bigger than rows_total - limit_num .

1 Comment

Yes true, but I don't want to use MySQL to randomize the rows, as I believe its not very efficient, and worsens as number of rows in the table increases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.