Hi, I'm trying to figure out how to count items in a foreach loop (whose length will constantly vary) and do something different when I reach the end. Here's the code:
$query = "INSERT INTO slotTest(courseID,weekID,dayID,timeID,stationID,open) VALUES (";
foreach ($station as $stationID) {
//check if station is open
if ($stationID != 'null') {
foreach ($time as $timeID) {
//check which time slots are open
if ($timeID != 'null') {
/* Count how many items are in this loop - how? */
$query .= "$course,$week,$day,$timeID,$stationID,1)";
/* If I'm at the end of the foreach loop break */
$query .= ",(";
}
}
}
}
$query .= ";";
echo $query;
Hopefully the comments between / / clarify exactly what I'm trying to do - figure out when I'm at the end of the $time foreach loop, and break out of it so the ,( characters don't get tacked on to the end of my $query string. It seems simple enough but I can't figure out what approach to use, so advice is much appreciated 🙂