indymx indymx - 7 months ago 16
SQL Question

Pushing elements onto an array not working as expected

I am trying to build dynamic arrays based on a field in my database. The purpose is so that I can compare those arrays to ensure that they don't each contain certain data. This is a race track scoring app, I need to make sure that race classes don't have entries in back to back classes.

The echo below outputs the data as I expected it would, however, when I try to push those rows from the database into arrays based on the class_id, it fails.

See the output below. What am I missing here?
(note, I didn't bring all of the data to this party, way too much to copy and paste)

function getEventClasses()
{
global $db;
global $event_id;
try {
$getEventClasses = $db->prepare('SELECT class_id
FROM tbl_event_classes
WHERE event_id = :event_id
ORDER BY sort');
$getEventClasses->bindValue(':event_id', $event_id);
$getEventClasses->execute();
} catch (PDOException $ex) {
echo 'An Error occurred!<br>';
//user friendly message
$ex->getMessage();
echo $ex;
exit;
}
$eventClasses = [];
while ($r = $getEventClasses->fetch(PDO::FETCH_ASSOC)) {
$eventClasses[] = [
'class_id' => $r['class_id']
];
}
return $eventClasses;
}
function getEntries($class_id)
{
global $db;
global $event_id;
global $f;
try {
$getEntries = $db->prepare('SELECT tbl_event_entries.racer_id,
tbl_event_entries.class_id,
tbl_event_entries.event_id,
tbl_event_classes.class_type,
tbl_event_classes.points_class,
tbl_moto_order.moto_nbr
FROM tbl_moto_order
INNER JOIN (tbl_event_entries
LEFT JOIN tbl_event_classes
ON (tbl_event_entries.event_id = tbl_event_classes.event_id)
AND (tbl_event_entries.class_id = tbl_event_classes.class_id))
ON (tbl_moto_order.class_id = tbl_event_entries.class_id)
AND (tbl_moto_order.event_id = tbl_event_entries.event_id)
WHERE tbl_event_entries.event_id = :event_id
AND tbl_event_entries.racer_id != "9999"
AND tbl_event_entries.class_id = :class_id');
$getEntries->bindValue(':event_id', $event_id);
$getEntries->bindValue(':class_id', $class_id);
$getEntries->execute();
} catch (PDOException $ex) {
echo 'An Error occurred!<br>';
//user friendly message
$ex->getMessage();
echo $ex;
exit;
}
$eventEntries = [];
while ($r = $getEntries->fetch(PDO::FETCH_ASSOC)) {
$eventEntries[] = [
'racer_id' => $r['racer_id'],
'class_id' => $r['class_id'],
'event_id' => $r['event_id'],
'class_type' => $r['class_type'],
'points_class' => $r['points_class'],
'moto_nbr' => $r['moto_nbr']
];
}
return $eventEntries;
}


$eventClasses = getEventClasses();
foreach ($eventClasses as $eventClass) {

$eventEntries = getEntries($eventClass['class_id']);

foreach ($eventEntries as $eventEntry) {

echo ' class_id : ', $eventClass['class_id'],
' | racer_id : ', $eventEntry['racer_id'],
' | event_id : ', $eventEntry['event_id'],
' | class_type : ', $eventEntry['class_type'],
' | points_class : ', $eventEntry['points_class'],
' | moto_nbr : ', $eventEntry['moto_nbr'],
'<br>';

array_push($runningClasses[$eventClass['class_id']],
$eventEntry['racer_id'],
$eventEntry['event_id'],
$eventEntry['class_type'],
$eventEntry['points_class'],
$eventEntry['moto_nbr']);

}

}


output of echo:

class_id : 17 | racer_id : 95 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 17 | racer_id : 350 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 17 | racer_id : 493 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 17 | racer_id : 295 | event_id : 326 | class_type : A | points_class : 17 | moto_nbr : 3
class_id : 14 | racer_id : 1142 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4
class_id : 14 | racer_id : 162 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4
class_id : 14 | racer_id : 151 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4
class_id : 14 | racer_id : 1176 | event_id : 326 | class_type : A | points_class : 14 | moto_nbr : 4


contents of $runningclasses:

Array
(
[17] => Array
(
[0] => 295
[1] => 326
[2] => A
[3] => 17
[4] => 3
)

[14] => Array
(
[0] => 1125
[1] => 326
[2] => A
[3] => 14
[4] => 4
)

)


I'm only getting the last eventEntry in each eventClass in the runningClasses array.

Thanks to Manjeet Barnala for reminding me of this.. Below is the working foreach loop I am now using:

foreach ($eventClasses as $eventClass) {
$eventEntries = getEntries($eventClass['class_id']);
foreach ($eventEntries as $eventEntry) {
$runningClasses[$eventClass['class_id']][] = [
$eventEntry['racer_id'],
$eventEntry['event_id'],
$eventEntry['class_type'],
$eventEntry['points_class'],
$eventEntry['moto_nbr']
];
}
}

Answer

replace this code with your array push and declare $runningClasses=array(); an empty array on the top of function..

$arr = array();
$arr['racer_id'] = $eventEntry['racer_id'];
$arr['event_id'] = $eventEntry['event_id'];
$arr['class_type'] = $eventEntry['class_type'];
$arr['points_class']= $eventEntry['points_class'];
$arr['moto_nbr']= $eventEntry['moto_nbr']);

$runningClasses[$eventClass['class_id']][] = $arr;