dongyuan1902 2015-12-08 15:01 采纳率: 0%
浏览 53
已采纳

嵌套的FOREACH语句不能按预期工作

In the first foreach statement, I have 4 attendeeid's in the attendees table.

In the second foreach, I have 1 attendeeid in the attend_date_temp table.

I'm trying to load a select box with names from the attendees table, less the one in the attend_date_temp table.

I thought that, since the first foreach would loop 4 times, the second foreach would also loop 4 times. But it doesn't. It loops one time, causing the code in the second foreach to not execute and load the select box with names.

How can this be written so that the second foreach loops 4 times like the first foreach so the select box will have the names loaded to it?

// 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

The attend_date_temp table:

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;

The history table:

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;

UPDATE: This is a small part of a payment posting page. Names are loaded in the select box based on the group selected, then payments are posted by the name selected. This not only posts their payments but also their attendance. Once all the money has been collected, the remaining names not selected are marked as absent.

However, there are group members that attend groups that are not their own. When they make a payment, their money is posted and attendance recorded. BUT, and that's what this is all about, when that same person's group gets selected for payments, I don't want that person's name to get loaded. He's already paid, and his attendance has already been updated. To have him load again and be processed wld corrupt the history table. So I have to keep from double loading the same person. That's why I'm trying to use this attend_date_temp table.

  • 写回答

1条回答 默认 最新

  • douhuai2861 2015-12-08 15:27
    关注

    Simple fix, use different variable names for the inner and outer loops:-

    /* 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>"; 
                    }  
                }  
            }  
        }
    

    To do a join you would do something like this:-

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

    EDIT

    Think it can be more simply done like this (not tested so please excuse any typos)

    <?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>"; 
            }  
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私