I am using codeigniter. In one of my models i have defined a function which retrieves data from database from two tables using UNION. I want my retrieved array of objects to be sorted on 'date_submitted' property. I tried to use the following code but it is not showing the desired result.
Here is my code
$this->db->select("*");
$this->db->from("artwork");
$this->db->get();
$query1 = $this->db->last_query();
$this->db->select("*");
$this->db->from("blog");
$this->db->get();
$query2 = $this->db->last_query();
$results = $this->db->query($query1." UNION ".$query2);
function cmp($a, $b)
{
if ($a->date_submitted == $b->date_submitted) {
return 0;
}
return ($a->date_submitted < $b->date_submitted) ? -1 : 1;
}
usort($results->result(), "cmp");
var_dump($results->result());
It gives the following output
array (size=7)
0 =>
object(stdClass)[31]
public 'id' => string '1' (length=1)
public 'user_id' => string '2' (length=1)
public 'title' => string 'My Painting2' (length=12)
public 'category' => string '1' (length=1)
public 'date_submitted' => string '2014-09-26 23:00:09' (length=19)
1 =>
object(stdClass)[32]
public 'id' => string '2' (length=1)
public 'user_id' => string '2' (length=1)
public 'title' => string 'Second artwork' (length=14)
public 'category' => string '2' (length=1)
public 'date_submitted' => string '2014-09-27 01:09:03' (length=19)
2 =>
object(stdClass)[33]
public 'id' => string '3' (length=1)
public 'user_id' => string '2' (length=1)
public 'title' => string 'Test Title' (length=10)
public 'category' => string '2' (length=1)
public 'date_submitted' => string '2014-10-12 01:19:34' (length=19)
3 =>
object(stdClass)[34]
public 'id' => string '4' (length=1)
public 'user_id' => string '2' (length=1)
public 'title' => string 'Test Title' (length=10)
public 'category' => string '3' (length=1)
public 'date_submitted' => string '2014-10-12 02:54:57' (length=19)
4 =>
object(stdClass)[35]
public 'id' => string '8' (length=1)
public 'user_id' => string '2' (length=1)
public 'title' => string 'test' (length=4)
public 'category' => string '3' (length=1)
public 'date_submitted' => string '2014-11-13 16:49:06' (length=19)
5 =>
object(stdClass)[36]
public 'id' => string '9' (length=1)
public 'user_id' => string '2' (length=1)
public 'title' => string 'Hello World' (length=11)
public 'category' => string '3' (length=1)
public 'date_submitted' => string '2014-12-06 22:15:46' (length=19)
6 =>
object(stdClass)[37]
public 'id' => string '5' (length=1)
public 'user_id' => string '2' (length=1)
public 'title' => string 'My first blog' (length=13)
public 'category' => string '2' (length=1)
public 'date_submitted' => string '2014-10-12 21:26:13' (length=19)
The first 6 rows are from first table and the last row is from 2nd table.
For the people who don't read comments. All that has to be done is adding the 'Order By' attribute from SQL to your query like so;
$results = $this->db->query($query1." UNION ".$query2 ." ORDER BY date_dubmitted")
You should first convert the date time string to unix timestamp and compare:
function cmp($a, $b)
{
$firstTimeStamp = strtotime( $a->date_submitted );
$secondTimeStamp = strtotime( $b->date_submitted );
if ( $firstTimeStamp == $secondTimeStamp ) {
return 0;
}
return ( $firstTimeStamp < $secondTimeStamp ) ? -1 : 1;
}