May 13 2013

Yii CGridView listdata with CArrayDataProvider and Pagination

Tagged Under :

Yii
Following last post CGridview listdata with CArrayDataProvider to list down the data with cgridview.

But it just can listing the records without the pagination. Use back last post example script and then modify the CArrayDataProvider as below example.

May 13 2013

Yii CGridView listdata with CArrayDataProvider

Tagged Under :

Yii
Yii, use CArrayDataProvider to listing out the array with CGridView widgets.

Below example are showing how to use it.
Data array:
$data = array();
$data[] = array('id' => 1, 'name' => 'name 1', 'age' => 22, 'date' => '1-Jan-2013');
$data[] = array('id' => 2, 'name' => 'name 2', 'age' => 25, 'date' => '1-Feb-2013');
$data[] = array('id' => 3, 'name' => 'name 3', 'age' => 22, 'date' => '1-Mac-2013');
$data[] = array('id' => 4, 'name' => 'name 4', 'age' => 28, 'date' => '1-Jan-2013');
$data[] = array('id' => 5, 'name' => 'name 5', 'age' => 23, 'date' => '1-Apr-2013');
$data[] = array('id' => 6, 'name' => 'name 6', 'age' => 29, 'date' => '1-May-2013');
$data[] = array('id' => 7, 'name' => 'name 7', 'age' => 21, 'date' => '1-Jan-2013');
$data[] = array('id' => 8, 'name' => 'name 8', 'age' => 20, 'date' => '1-Feb-2013');

May 11 2013

Mysql better way to select random records

Tagged Under : ,

MySQL
The easiest way to generate random records in Mysql is to use the ORDER BY RAND().
SELECT col1 FROM tbl ORDER BY RAND() LIMIT 1;
This can work fine for small tables. However, for big table, it will have a serious performance problem as in order to generate the list of random rows, because Mysql need to assign random number to each row and then sort them.

Even if you only want random 1 records from 200k+ rows, Mysql need to sort all the records and then extract it.