将元素推送到不按预期工作的数组上

时间:2016-04-27 10:15:32

标签: php mysql arrays

我正在尝试基于数据库中的字段构建动态数组。目的是让我可以比较这些数组,以确保它们不包含某些数据。这是一个赛道评分应用程序,我需要确保比赛类没有背靠背类的条目。

下面的echo会按照我的预期输出数据,但是,当我尝试将这些行从数据库推送到基于class_id的数组时,它会失败。

请参阅下面的输出。我在这里错过了什么? (注意,我没有将所有数据带到这个聚会上,复制和粘贴太多了)

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']);

        }

}

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

$ 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
    )

)

我只在runningClasses数组中的每个eventClass中获取最后一个eventEntry。

感谢Manjeet Barnala提醒我这一点。以下是我正在使用的工作foreach循环:

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']
        ];
    }
}

1 个答案:

答案 0 :(得分:1)

用数组push替换此代码,并在函数顶部声明$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; 一个空数组。

def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond TekEvent.list(params), model:[tekEventCount: TekEvent.count()]
    }