Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Array $_POST loop issues

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: enter image description here

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: enter image description here

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.

like image 433
drewrockshard Avatar asked May 06 '26 20:05

drewrockshard


2 Answers

the solution below works for a list of checkboxes with the same name. i believe it will work for a list of drop-down menus as well, as i wrote below, but i haven't tested it.

<select name="monworkhours[]">
    <option value="aaa">aaa</option>
    <option value="bbb">bbb</option>
    <option value="ccc">ccc</option>
</select>

<select name="monworkhours[]">
    <option value="ddd">ddd</option>
    <option value="eee">eee</option>
    <option value="fff">fff</option>
</select>

once you submit this form, $_POST['monworkhours'] will hopefully be an array. i haven't tested it, but i dont see why this wouldn't work.

like image 71
Mauro Avatar answered May 08 '26 09:05

Mauro


Looping through $_POST['monworkhours'] is trivial:

foreach ($_POST['monworkhours'] as $i => $value){
    // Do something
}

Without seeing more code, I can't say what your problem is. Are you selecting values in the HTML (e.g. <option value="7am7pm" selected>7AM-7PM</option>), or with javascript?

If it's javascript, it may have to do with the fact that you're using the same ID for multiple elements. This is technically incorrect. You could use sequentially numbered IDs, like so:

<select name="monshifthours_0" id="monshifthours_0">
...
<select name="monshifthours_1" id="monshifthours_1">

And then iterate through them, in PHP, like so:

$i = 0;
while (isset($_POST["monshifthours_$i"])){
    // Do something
    $i++;
}

If none of this helps, post more code. Much more.

like image 45
Patrick Fisher Avatar answered May 08 '26 09:05

Patrick Fisher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!