嵌套的FOREACH语句无法正常工作

时间:2015-12-08 15:01:13

标签: php mysql foreach

在第一个foreach声明中,我在attendeeid表格中有4个attendees

在第二个foreach中,attendeeid表中有attend_date_tempselect box

我尝试使用attendees表中的名称加载attend_date_temp,而不是foreach表中的名称。

我认为,由于第一个foreach会循环4次,第二个foreach也会循环4次。但它并没有。它循环一次,导致第二个select box中的代码无法执行并加载带有名称的foreach

如何编写这样的内容,以便第二个foreach循环4次,就像第一个select box一样// Load Button Clicked if(isset($_POST['loadnames'])) { /* Read the history file and get the last record for each attendee for a particular group and a particular member and write them to the attend_date_temp table if attend_date = CURDATE().*/ $stmt = $db->prepare('SELECT historyid, attend_date, attendeeid, groupid, memberid FROM history WHERE groupid = :groupid AND memberid = :memberid AND attend_date = CURDATE() ORDER BY historyid DESC LIMIT 1'); $stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT); $stmt->bindValue(':memberid', $_SESSION['memberid'], PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); foreach($result as $row) { $aid = $row[2]; // set the attendeeid $stmt = $db->prepare('INSERT INTO attend_date_temp (attendeeid, groupid, memberid) VALUES(:aid, :gid, :mid)'); $stmt->bindValue(':aid', $aid, PDO::PARAM_INT); $stmt->bindValue(':gid', $_POST['groupid'], PDO::PARAM_INT); $stmt->bindValue(':mid', $_SESSION['memberid'], PDO::PARAM_INT); $stmt->execute(); } $aaa = 0; // used to set the first select box entry to "Select" /* Load the Select Box with names, less the ones found in attend_date_temp Table. */ $stmt = $db->prepare('SELECT a.attendeeid, fname, lname, a.groupid, a.memberid, s.attendeeid, suspend FROM attendees AS a JOIN suspended AS s ON a.attendeeid = s.attendeeid WHERE a.memberid = :memberid AND suspend = "N" AND a.groupid = :groupid ORDER BY lname'); $stmt->bindValue(':memberid', $_SESSION["memberid"], PDO::PARAM_INT); $stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); foreach($result as $row){ echo '<script type="text/javascript">alert("In the first loop"); </script>'; $aid = $row[0]; $lname = $row[2]; $fname = $row[1]; $stmt = $db->prepare('SELECT attendeeid, memberid FROM attend_date_temp WHERE groupid = :groupid AND attendeeid = :aid'); $stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT); $stmt->bindValue(':aid', $aid, PDO::PARAM_INT); $stmt->execute(); $result2 = $stmt->fetchAll(); foreach ($result2 as $row2) { echo '<script type="text/javascript">alert("In the second loop"); </script>'; // evaluate attendees attendeeid against attend_date_temp attendeeid if($row2['attendeeid'] != $aid){ // Load the flush Table with the IDs from the selected group if($_SESSION['flush'] == 0) { $stmt = $db->prepare('INSERT INTO flush (attendeeid, memberid) VALUES(:attendeeid, :memberid)'); $stmt->bindValue(':attendeeid', $aid, PDO::PARAM_INT); $stmt->bindValue(':memberid', $_SESSION['memberid'], PDO::PARAM_INT); $stmt->execute(); } if($aaa == 0) { echo "<option value='Select'>Select</option>"; echo "<option value=".$aid.">".$lname.", ". $fname."</option>"; $aaa = 1; } else { echo "<option value=".$aid.">".$lname.", ". $fname."</option>"; } } } } $_SESSION['flush'] = 1; exit(); } // last brace: loadnames 将加载名称?

DROP TABLE IF EXISTS `attend_date_temp`;
CREATE TABLE `attend_date_temp` (
`attendeeid` int(10) unsigned NOT NULL,
`groupid` int(10) unsigned NOT NULL,
`memberid` int(10) unsigned NOT NULL,
KEY `attendeeid` (`attendeeid`),
KEY `memberid` (`memberid`),
CONSTRAINT `attend_date_temp_ibfk_1` FOREIGN KEY (`attendeeid`)  REFERENCES `attendees` (`attendeeid`) ON DELETE CASCADE,
CONSTRAINT `attend_date_temp_ibfk_2` FOREIGN KEY (`memberid`)   REFERENCES `members` (`memberid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

attend_date_temp表:

DROP TABLE IF EXISTS `history`;
CREATE TABLE `history` (
`historyid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`amount` float NOT NULL,
`subsidy` char(1) NOT NULL,
`last_payment` date NOT NULL,
`amount_paid` float NOT NULL,
`balance` float NOT NULL,
`attend` char(1) NOT NULL DEFAULT 'N',
`attend_date` date NOT NULL,
`groupid` char(1) NOT NULL,
`attendeeid` int(10) unsigned NOT NULL,
`memberid` int(10) unsigned NOT NULL,
PRIMARY KEY (`historyid`),
KEY `attendeeid` (`attendeeid`),
CONSTRAINT `history_ibfk_15` FOREIGN KEY (`attendeeid`) REFERENCES `attendees` (`attendeeid`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

历史表:

select box

更新 这是付款过帐页面的一小部分。根据所选的组在my $content = $xml->XMLin("filesmap.xml")->{Item}; my %files = map { $_->{Path} => 1 } @$content; 中加载名称,然后按所选名称过帐付款。这不仅可以发布他们的付款,还可以发布他们的付款。收到所有款项后,未选择的剩余名称将被标记为不存在。

但是,有些团体成员参加不属于他们自己的团体。当他们付款时,他们的钱被张贴并且出勤率被记录下来。但是,这就是这一切,当同一个人的团体被选中付款时,我不希望这个人的名字被加载。他已经付了钱,他的出勤率已经更新了。让他再次加载并被处理将破坏历史表。所以我必须避免双重加载同一个人。这就是为什么我试图使用这个attend_date_temp表。

1 个答案:

答案 0 :(得分:1)

简单修复,为内部和外部循环使用不同的变量名称: -

/* Load the Select Box with names, less the ones found in attend_date_temp Table. */
    $stmt = $db->prepare('SELECT a.attendeeid, fname, lname, a.groupid, a.memberid, s.attendeeid, suspend
                            FROM attendees AS a
                            JOIN suspended AS s ON a.attendeeid = s.attendeeid 
                            WHERE a.memberid = :memberid
                            AND suspend = "N"
                            AND a.groupid = :groupid
                            ORDER BY lname');
    $stmt->bindValue(':memberid', $_SESSION["memberid"], PDO::PARAM_INT);
    $stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT);
    $stmt->execute();
    $result = $stmt->fetchAll();
    foreach($result as $row){
        echo '<script type="text/javascript">alert("In the first loop"); </script>';

        $aid = $row[0];
        $lname = $row[2];
        $fname = $row[1];
        $stmt = $db->prepare('SELECT attendeeid, memberid
                                FROM attend_date_temp
                                WHERE groupid = :groupid
                                AND attendeeid = :aid');
        $stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT);
        $stmt->bindValue(':aid', $aid, PDO::PARAM_INT);
        $stmt->execute();
        $result2 = $stmt->fetchAll();
        foreach ($result2 as $row2) { 
            echo '<script type="text/javascript">alert("In the second loop"); </script>';

            // evaluate attendees attendeeid against attend_date_temp attendeeid
            if($row2['attendeeid'] != $aid){ 

                // Load the flush Table with the IDs from the selected group
                if($_SESSION['flush'] == 0) {
                    $stmt = $db->prepare('INSERT INTO flush (attendeeid, memberid)
                                                    VALUES(:attendeeid, :memberid)');
                    $stmt->bindValue(':attendeeid', $aid, PDO::PARAM_INT);
                    $stmt->bindValue(':memberid', $_SESSION['memberid'], PDO::PARAM_INT);
                    $stmt->execute();
                } 
                if($aaa == 0) {
                    echo "<option value='Select'>Select</option>";
                    echo "<option value=".$aid.">".$lname.", ". $fname."</option>"; 
                    $aaa = 1;
                } else { 
                    echo "<option value=".$aid.">".$lname.", ". $fname."</option>"; 
                }  
            }  
        }  
    }

要进行连接,您可以执行以下操作: -

$stmt = $db->prepare('SELECT a.attendeeid, fname, lname, a.groupid, a.memberid, s.attendeeid, suspend, adt.attendeeid AS adt_attendeeid, adt.memberid AS adt_memberid
                        FROM attendees AS a
                        INNER JOIN suspended AS s ON a.attendeeid = s.attendeeid 
                        LEFT OUTER JOIN attend_date_temp adt ON adt.groupid = a.groupid AND adt.attendeeid = a.attendeeid
                        WHERE a.memberid = :memberid
                        AND suspend = "N"
                        AND a.groupid = :groupid
                        AND adt.groupid IS NULL
                        ORDER BY lname');
$stmt->bindValue(':memberid', $_SESSION["memberid"], PDO::PARAM_INT);
$stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT);
$stmt->execute();

修改

认为它可以更简单地完成(没有经过测试,请原谅任何错别字)

<?php

    $first = true;

/* Load the Select Box with names, less the ones found in attend_date_temp Table. */
    $stmt = $db->prepare('SELECT a.attendeeid, fname, lname
                            FROM attendees AS a
                            INNER JOIN suspended AS s ON a.attendeeid = s.attendeeid 
                            LEFT OUTER JOIN attend_date_temp adt ON adt.groupid = a.groupid AND adt.attendeeid = a.attendeeid
                            WHERE a.memberid = :memberid
                            AND suspend = "N"
                            AND a.groupid = :groupid
                            AND adt.groupid IS NULL
                            ORDER BY lname');
    $stmt->bindValue(':memberid', $_SESSION["memberid"], PDO::PARAM_INT);
    $stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT);
    $stmt->execute();
    $result = $stmt->fetchAll();
    foreach($result as $row)
    {
        $aid = $row[0];
        $lname = $row[2];
        $fname = $row[1];

        // Load the flush Table with the IDs from the selected group
        if($_SESSION['flush'] == 0) 
        {
            $stmt = $db->prepare('INSERT INTO flush (attendeeid, memberid)
                                            VALUES(:attendeeid, :memberid)');
            $stmt->bindValue(':attendeeid', $aid, PDO::PARAM_INT);
            $stmt->bindValue(':memberid', $_SESSION['memberid'], PDO::PARAM_INT);
            $stmt->execute();
        } 
        if($first) 
        {
            echo "<option value='Select'>Select</option>";
            echo "<option value='".$aid."'>".$lname.", ". $fname."</option>"; 
            $first = false;
        } 
        else 
        { 
            echo "<option value='".$aid."'>".$lname.", ". $fname."</option>"; 
        }  
    }