Posts Tagged with sort

Displaying 1-4 of 4 results.
Wiki: Yii 1.1 Cgridview TextField Search and Filter of a relations field without errorswiki
posted by admin on October 11, 2016
1) Add some public attribute to the model.

class Customer extends CActiveRecord
{
public $customername; // for firstname + lastname;
....

2) Declare it safe on search.

public function rules()
{
return array(
....
array('id, firstname, lastname, ...., customername', 'safe', 'on'=>'search'),
);
}

3) Set the label for it.

public function attributeLabels()
{
return array(
'id' => 'ID',
'firstname' => 'First Name',
'lastname' => 'Last Name',
....
'customername' => 'Customer Name',
);
}

4) Add a search condition for it in "search()" method.

public function search()
{
$criteria=new CDbCriteria;

$criteria->compare('id',$this->id);
$criteria->compare('firstname',$this->firstname);
$criteria->compare('lastname',$this->lasttname);
....
if ( $this->customername != "" )
{
$crit2 = new CDbCriteria;
$crit2->compare('firstname', $this->customername , true, 'OR');
$crit2->compare('lastname', $this->customername , true, 'OR');
$criteria->mergeWith($crit2);
}
....

5) Add the sorting rule for it, also in the "search()" method.

....
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'lastname, firstname',
'attributes' => array(
'customername' => array(
'asc' => 'lastname, firstname',
'desc' => 'lastname DESC, firstname DESC',
),
'*',
),
),
'pagination' => array(
'pageSize' => 20,
),
));

6) And use the attribute in the grid.

$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'customer-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
....
array(
'name' => 'customername',
'value' => '$data->firstname . " " . $data->lastname',
),
....
Read more
Wiki: PHP 5 Sorting Arrayswiki
posted by admin on April 16, 2016
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.

PHP - Sort Functions For Arrays

In this chapter, we will go through the following PHP array sort functions:
  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key


Sort Array in Ascending Order - sort()
The following example sorts the elements of the $cars array in ascending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
sort($cars);
?>


The following example sorts the elements of the $numbers array in ascending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>


Sort Array in Descending Order - rsort()
The following example sorts the elements of the $cars array in descending alphabetical order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>


The following example sorts the elements of the $numbers array in descending numerical order:

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>


Sort Array (Ascending Order), According to Value - asort()
The following example sorts an associative array in ascending order, according to the value:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
?>


Sort Array (Ascending Order), According to Key - ksort()
The following example sorts an associative array in ascending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>


Sort Array (Descending Order), According to Value - arsort()
The following example sorts an associative array in descending order, according to
arsort($age);
?>
[/php]

Sort Array (Descending Order), According to Key - krsort()
The following example sorts an associative array in descending order, according to the key:

<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
Read more
Wiki: PHP Dominant Colour Function with GD Librarywiki
posted by admin on April 13, 2016
Someone asked if it was possible to find the dominant colour (the more frequent colour, in %) of an image.

So I read about the GD PHP Library, which enables image support. I found the interesting function imageColorAt() which the colour of a specific pixel. Hence, my function consists of resizing to a known size, 100 x 100, then looping into each pixel, storing it’s colour into an array, then sorting the array to find the more frequent key.

Here’s the result :

function getDominantColour($image, $type='jpeg') // $type can be gif, jpeg or png
{
// This function needs GD Library enabled

eval('$source = imagecreatefrom' . $type . '("' . $image . '");'); // we load the source image
$source_x = imagesx($source); // source's X
$source_y = imagesy($source); // source's Y
$resized_x = 100; // resized image's X
$resized_y = 100; // resized image's Y
$resized= imagecreatetruecolor($resized_x, $resized_y); // we create a blank 100 x 100 image
imagecopyresampled($resized, $source, 0, 0, 0, 0, $resized_x, $resized_y, $source_x, $source_y); // We then copy and resized the source image into the blank image

// Required vars declaration
$colours = array();
$rgb = '';
$index = array();
for ($x=0; $x<100; $x++) // We loop into the pixels X
{
for ($y=0; $y<100; $y++) // We loop into the pixels Y
{
$rgb = imagecolorat($resized, $x, $y); // get the RGB of the pixel
$index = imagecolorsforindex($resized, $rgb); // Get the RGB Index
$key = 'R' . $index['red'] . 'G' . $index['green'] . 'B' . $index['blue']; // Put it into a string
if (empty($colours[$key])) // If it's the first time this colour is encountered
{
$colours[$key] = 1; // We give it a value of 1
} else { // If it's not the first time it is encountered
$colours[$key]++; // We increment its value
}
}
}
arsort($colours, SORT_NUMERIC); // We then sort the colours array so that the first one is the most frequent one
return key($colours); // we return the most frequent value
}


Working with images sure is fun. Looking forward to have more functions with the GD libraryRead more
Resolved: Sort divs bases on element's data attribute
posted by admin on December 22, 2015
If I have several divs:

<div data-sort='4'>div4</div>
<div data-sort='8'>div8</div>
<div data-sort='1'>div1</div>
<div data-sort='19'>div19</div>
<div data-sort='12'>div12</div>

Actually I want to sort divs by data attribute by sorting ASC and DESCRead more