Reputation: 99
I've got a massive $events
.
To output information of this massive I do:
<?php foreach ($this->events as $usern): ?><?php foreach ($usern as $user): ?>
<tr>
<td><?php echo $user['id']; ?></td>
<td><?php echo $user->resource_types; ?></td>
<td><?php echo $user->resource_ids; ?></td>
<td><?php echo $user->action; ?></td>
<td><?php echo $this->owner($user->originator); ?></td>
<td><?php echo $user->event_date; ?></td>
<td>
<?php //if($this->canManageBusiness($user->id)): ?>
<?php // endif ?>
</td>
</tr>
<?php endforeach ?><?php endforeach ?>
Interesting that I may use both $user->id
and $user['id']
sintax. But never mind.
How can I sort this massive by $user['id']
desc?
Edited: I take data from database, but not usual way, so I can't use sql syntax so sort results:
$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()->from($this->_dbTable, array('id','resource_types','resource_ids','action','originator','event_date'))->where('resource_types = ?', 'business')->where('resource_ids',$bus->id));
$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()->from($this->_dbTable, array('id','resource_types','resource_ids','action','originator','event_date'))->where('resource_types = ?', 'document-type')->where('resource_ids',$dt->id));
return $events
Upvotes: 0
Views: 136
Reputation: 99
Decided to denormolise my DB Table to make the process of selection data easier. Let's see if it get's better.
Upvotes: 0
Reputation: 64657
This sucks, since you have to go through the list twice, but you could...
$events_arr = array();
<?php foreach ($this->events as $usern): ?><?php foreach ($usern as $user): ?>
$events_arr[$user_id][] = $usern; endforeach; endforeach;
ksort($events_arr);
Now you can use events arr, which is sorted by user_id
foreach ($events_arr as $user_id => $usern) {
foreach ($usern as $user) {
echo $user_id . ": " . $user->action;
}
}
Upvotes: 0
Reputation: 8519
adding an order()
to the query doesn't get you there?
$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()
->from($this->_dbTable,
array('id','resource_types','resource_ids','action','originator','event_date'))
->where('resource_types = ?', 'business')
->where('resource_ids',$bus->id)
->order('id DESC'));
$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()
->from($this->_dbTable,
array('id','resource_types','resource_ids','action','originator','event_date'))
->where('resource_types = ?', 'document-type')
->where('resource_ids',$dt->id)
->order('id DESC'));
return $events
it looks to me like this is all coming from the same table, if so why not build a single query?
Maybe something like:
$events[] = $this->_dbTable->fetchAll($this->_dbTable->select()
->from($this->_dbTable,
array('id','resource_types','resource_ids','action','originator','event_date'))
->where('resource_types IN (?)', array('business', 'document-type'))
->where('resource_ids IN (?)', array($bus->id, $dt->id))
->order('id DESC'));
Not real sure if this will help get you there, maybe.
Upvotes: 1
Reputation: 23777
Do you read the data from a database? then simply add to your query SORT BY id DESC
else you may use usort() in PHP:
usort($events, function ($a, $b) { return $a->id - $b->id; });
Upvotes: 2