Welcome Guest, Not a member yet? Register   Sign In
How can I sort an array of objets?
#1

[eluser]Sein Kraft[/eluser]
I've the next array:
Code:
Array
(
    [0] => stdClass Object
        (
            [g_id] => cejiifyniu
            [g_user] => 1
            [g_type] => image
            [g_rating] => General
            [g_title] => Fairy Tale
            [g_description] => A puppy of something
            [g_date] => 1239811551
            [g_rname] => fairy_tale.png
            [g_nname] => seinkraft.cejiifyniu.fairy_tale
            [g_ext] => .png
            [g_height] =>
            [g_width] =>
        )

    [1] => stdClass Object
        (
            [g_id] => skhjmpkkhf
            [g_user] => 1
            [g_type] => image
            [g_rating] => General
            [g_title] => Hello
            [g_description] => White thing...
            [g_date] => 1239813280
            [g_rname] => hello.png
            [g_nname] => seinkraft.skhjmpkkhf.hello
            [g_ext] => .png
            [g_height] =>
            [g_width] =>
        )

)

The code who generate that:
Code:
$cosoTemporal = array();
        
foreach($nodes as $node) {
$data_query = $this->db
                   ->from('gallery')
                   ->where('g_id', $node->g_id)
                   ->limit($config_pagi['per_page'],$offset)
                   ->get();
        
  foreach($data_query->result() as $resultElement)
  {
    array_push($cosoTemporal, $resultElement);
  }//ENDFOREACH
  print_r($cosoTemporal);
}//ENDFOREACH

I've read this but i can't implement. http://ellislab.com/forums/viewthread/53143/
#2

[eluser]xwero[/eluser]
why not sort the array in the sql statement using order by?
#3

[eluser]obiron2[/eluser]
I've got some code to do this at home. I'll try to dig it out tonight.

Obiron
#4

[eluser]Sein Kraft[/eluser]
@xwero because $nodes hasn't the posivility of order, is an array which contains random strings. And the unique way to order that is sorting $data_query.
#5

[eluser]xwero[/eluser]
Sein Kraft if i get it you want to get a certain number of pictures from different galleries to be displayed on one page? And i guess you want the pictures to be sorted by name? Could you give some more information how you want to sort?
#6

[eluser]Sein Kraft[/eluser]
$nodes is an array who contains items with the id of the image on the table gallery right? Well that id is catptured from another table, not from the table galleries. So the ids of the images in $nodes isn't sorted asc or desc because the order in the table is randomless.

So we had the array $nodes who contain ids of the pictures in random order with no posibility toorder because the ids are an alphabetical random string of 10 characters (ex. "shgemuanmn").

So the unique posibility to order the images is order the array $cosoTemporal by the key [g_date] (unix date) right?

The array showed in the first post is print_r($cosoTemporal);
#7

[eluser]xwero[/eluser]
Lets do some database trickery Wink
Code:
// array to catch the selects
$sql_arr = array();
foreach($nodes as $node) {
   // build select statement
   $sql_arr[] = '(SELECT * FROM gallery WHERE g_id = "'.$node->g_id.'" LIMIT '.$offset.','.$config_pagi['per_page'].')';
}
// glue selects wih union and add the order by
$sql_str = implode(' UNION ',$sql_arr).' ORDER BY g_date';

return $this->db->query($sql_str)->results();
#8

[eluser]Sein Kraft[/eluser]
What the he...xD I ven't see anything like this before xD

I will try but the performance...is going down
#9

[eluser]Sein Kraft[/eluser]
This my sql querry:
Code:
$query_images = $this->db
                     ->from('node')
                     ->where($where, $value)
                     ->get();

    $cosoTemporal = array();
        
        foreach($query_images->result() as $node) {
        $data_query = $this->db
                           ->from('gallery')
                           ->where('g_id', $node->g_id)
                           ->order_by('g_date', 'desc')
                           ->limit($images, $offset)
                           ->get();
        
            foreach($data_query->result() as $resultElement)
            {
                array_push($cosoTemporal, $resultElement);
            }//ENDFOREACH
        
        $data['query_files'] = $cosoTemporal;
        }//ENDFOREACH
    
    return $cosoTemporal;
#10

[eluser]xwero[/eluser]
haven't you tested my snippet?

the sql statement will look like this mysql example
Code:
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
I changed it it fit your needs so it isn't that that difficult.

If you worry about the performance i think the performance loss using union will be compensated by the fact it's one query where your code runs multiple queries to get the pictures per gallery.




Theme © iAndrew 2016 - Forum software by © MyBB