I have a db-table called vm_kupon
. It looks a little like this:
|id |uid |bet |
|1 |20 |1 |
|2 |20 |2 |
|1 |21 |X |
|2 |21 |1 |
|1 |22 |X |
|2 |22 |X |
I would like to show these bets, but I would like it to group the rows with the same uid
. Right now I'm doing it like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$date = Date('j');
$query
->SELECT(array('a.*', 'b.*', 'c.*'))
->FROM($db->quoteName('vm_kampe', 'a'))
->JOIN('INNER', $db->quoteName('vm_kuponer', 'b') . ' ON (' . $db->quoteName('a.id') . ' = ' . $db->quoteName('b.id') . ')')
->JOIN('INNER', $db->quoteName('vm_tabel', 'c') . ' ON (' . $db->quoteName('b.uid') . ' = ' . $db->quoteName('c.uid') . ')')
->WHERE($db->quoteName('a.dato') . ' = ' . $db->quote($date) . ' AND ' . $db->quoteName('c.liga') . ' = ' . $db->quote('bs'));
$db->setQuery($query);
$results = $db->loadObjectList();
Then I tried adapting the answer to my code, but I'm not that good.
//INSTANTIATE NEW EMPTY ARRAY
$arr = array();
//LOOP THROUGH RESULTS AND GROUP THEM INTO NEW ARRAY BASED ON UID AS ARRAY KEY
foreach($results as $row){
$arr[$row->uid][] = $row;
}
//SORT UIDS ARRAY BASED ON KEY VALUE ASCENDING... so keys are 20, 21, 22, etc...
ksort($arr);
//LOOP THROUGH NEW ARRAY TO CREATE YOUR DISPLAY
foreach ($arr as $uidGroup => $bets){
echo "HERE IS THE GROUP YOU ARE CURRENTLY DISPLAYING<br />";
echo 'Group - '.$uidGroup.'<br />';
foreach($bets as $b){
echo '<div class="dagens_kampe_kupon">ID: '.$b->id.' - Bet: '.$b->bet.'</div>';
}
};