I currently have a clunky and long-winded solution that I want to significantly streamline.
Without pasting excessive code, I'll just try to precisely describe what I'm doing and what I would like to do.
The page is used to add a club member, or a group of club members, to a specific event. There is the 'musicians' table with all members, and 'attendance' table with all attendance records for events. I also have a 'Rehearsals' table, with specifics for each event. 'Attendance' table has three columns: 'attCode', 'mus_number' and 'rhcode'.
A form on the page gives the user a drop-down menu. The choices allow to insert a group of members (four sections are offered), or all members, or any one individual member.
So, the code checks for GET or POST; if there's GET, it shows the form; if there's POST, it executes the INSERT code and redirects to a different page.
So, we first GET the rhcode (event code), then SELECT all members from the "members" table (I use that to populate the drop-down menu). We then check for POST, and if there's data, we go through series of IF / ELSEIF evaluations, checking which option was submitted from the drop-down menu. For each of the sections I create a separate SELECT query, WHERE section_ID is a specific value (based on a value from POST), and then iterate through the resulting array, building an INSERT query from the results, like this:
if (isset($musnumber) && ($musnumber == .2)) { // if lesser than 1, it's a section and not an individual
$query_Musicians = "SELECT `number` FROM musicians WHERE instr_section == 20";
$Musicians = mysqli_query($dbConnection, $query_Musicians) or die(mysqli_error());
$insertSQL = "INSERT INTO attendance (musician_number, rhcode) VALUES ";
$i = 0;
while ($row_Musicians = mysqli_fetch_assoc($Musicians)) {
if ($i>0) {$insertSQL .= ", ";}
$insertSQL .= "(".$row_Musicians['number'].", '".$rhcode."')";
$i++;
}
$Result1 = mysqli_query($dbConnection, $insertSQL) or die(mysqli_error());
}
I have this block repeat four times, once for each section (.3 for section 30, .4 for section 40, .7 for the entire list), and once more, if the '$musnumber' value form the POST >= 1 (meaning, it is an individual member, rather than a group). For that one, I simply INSERT that member info (member number, event number).
I've been trying to simplify this. My goal is, if possible, to do it in a single block. The logic is this:
IF THEN
.1 INSERT into table WHERE source data Section == 10
.2 INSERT into table WHERE source data Section == 20
.3 INSERT into table WHERE source data Section == 30
.4 INSERT into table WHERE source data Section == 40
.7 INSERT into table WHERE source data Section == [anything] -- in other words, the whole list
>=1 INSERT a single record
So, while iterating through the source records, I would have to check for that source 'Section' column and, depending on the value submitted, only pick specific ones to add to the INSERT string.
Does anyone have a suggestion how to build that INSERT query in a single, simpler and more elegant block of code, rather than multiple separate IF / ELSEIF evaluations?