I'm going to ask a question regarding an issue I'm having and try to figure this out based on input from your guys. I'll post source code if I REALLY can't get it, but here I go ...
So, I have a form, which displays fields vertically. Everything is a drop down menu, and at the very end is a submit button. There's a sprinkle of javascript that allows me to add a new row without a page refresh. So, there's never the same amount of $_POST arrays for each key. The key I'm worried about (well all of them, but once I get it working, it will work for all of them) is the $_POST['monworkhours']
drop down. This is a drop down that has a listing of different work hours. I believe the problem lies in the fact I need to loop through all the $_POST['monworkhours']
array based on the submission. I don't exactly know how to do this.
Additionally, the "problem" is causing the same results for each row of output. So whatever I set for the first field ends up being the result for every additional row I have "added" via the javascript function.
Any help is appreciated.
The Form:
<select name="monshifthours[]" id="monshifthours">
<option value="OFF">OFF</option>
<optgroup label="Front/Back Half">
<option value="7am7pm">7AM-7PM</option>
<option value="7pm7am">7PM-7AM</option>
<option value="7am7pmalt">7AM-7PM (Alt)</option>
<option value="7pm7amalt">7PM-7AM (Alt)</option>
</optgroup>
<optgroup label="Monday - Friday">
<option value="630am330pm">630AM-330PM</option>
<option value="7am4pm">7AM-4PM</option>
<option value="8am5pm">8AM-5PM</option>
<option value="10am7pm">10AM-7PM</option>
</optgroup>
</select>
The $_POST output (2 form rows):
["monshifthours"]=>
array(2) {
[0]=>
string(6) "7am7pm"
[1]=>
string(6) "7pm7am"
}
Screenshot:
getCellColor() Function:
function getCellColor($dow) {
foreach($_POST[$dow . 'shifthours'] as $key=> $hour) {
echo $count;
if ($hour == "7am7pmalt") {
return "style=\"background: yellow; color:#000;\"";
}
elseif ($hour == "OFF") {
return "style=\"background: red; color:#fff;\"";
}
else {
return "style=\"background: green; color:#fff;\"";
}
}
}
For Submission Output:
if (isset($_POST['submit'])) {
echo preTableFrmt();
foreach($engineer as $a => $b) {
echo "| [[$engineer[$a]]] || ".getCellColor('mon')." | $monday[$a] || ".getCellColor('tues')." | $tuesday[$a] || ".getCellColor('wed')." | $wednesday[$a] || ".getCellColor('thur')." | $thursday[$a] || ".getCellColor('fri')." | $friday[$a] || ".getCellColor('sat')." | $saturday[$a] || ".getCellColor('sun')." | $sunday[$a] <br />|-<br />";
}
echo postTableFrmt();
}
else { echo "Waiting for data..."; }
Note: When submitting one row of the form, everything is fine; it's when I have more than one; then I get duplicate information.
The following example should show you what happens when I "add an engineer" (making it now two form rows). I'll post the output after the image:
Application Image:
Note: Don't pay attention to the output of the times, as they are being formatted on the fly through some regex, and some of them work and some do not as I have to finish writing those functions. Pay attention to the fact that the following output I send is correct on the first section, but the second section is a duplicate of the first line of output:
|-
| [[Drew Decker]] || style="background: yellow; color:#000;" | 7am7pmalt || style="background: red; color:#fff;" | OFF || style="background: red; color:#fff;" | OFF || style="background: red; color:#fff;" | OFF || | || style="background: red; color:#fff;" | OFF || style="background: red; color:#fff;" | OFF
|-
| [[Drew Decker]] || style="background: yellow; color:#000;" | 7pm-7am || style="background: red; color:#fff;" | OFF || style="background: red; color:#fff;" | OFF || style="background: red; color:#fff;" | OFF || | || style="background: red; color:#fff;" | OFF || style="background: red; color:#fff;" | OFF
Note: The duplicates are here the style="background: yellow; color:#000;"
.
My Overall Solution
So, I ended up figuring out a solution that didn't require me to change my form code. I don't believe I needed to change my form code since it is dynamic. Normally, if I was writing this to a database and submitting multiple rows of form data to the database, I can see where this is important, however, in this situation, I do not believe that it was the proper solution. However, I do thank and appreciate all the thoughtful and informational answers I received.
I ended up changing the getCellColor()
function to the following:
function getCellColor($index,$dow) {
if ( isset($_POST[$dow . 'shifthours'][$index]) && ($_POST[$dow . 'shifthours'][$index] == "7am7pmalt" || $_POST[$dow . 'shifthours'][$index] == "7pm7amalt")) {
return "style=\"background: yellow; color:#000;\"";
}
elseif ($_POST[$dow . 'shifthours'][$index] == "OFF") {
return "style=\"background: red; color:#fff;\"";
}
else {
return "style=\"background: green; color:#fff;\"";
}
}
My foreach
now looks like:
foreach($engineer as $a => $b) {
echo "|-<br />| [[$engineer[$a]]] || ".getCellColor($a,"mon")." | ".format_date($monday[$a])." || ".getCellColor($a,"tues")." | ".format_date($tuesday[$a])." || ".getCellColor($a,"wed")." | ".format_date($wednesday[$a])." || ".getCellColor($a,"thur")." | ".format_date($thursday[$a])." || ".getCellColor($a,"fri")." | ".format_date($friday[$a])." || ".getCellColor($a,"sat")." | ".format_date($saturday[$a])." || ".getCellColor($a,"sun")." | ".format_date($sunday[$a])."<br />";
}
Doing it this way ensures that I just tell it to select the correct array index, and proceed accordingly.