PHP Content Issues: Array $ _POST

I am going to ask a question about the problem that I have, and try to figure it out based on your guys. I will send the source code if I REALLY cannot get it, but here I go ...

So, I have a form that displays the fields vertically. All this is a drop-down menu, and at the very end is the submit button. There is sprinkled with javascript, which allows me to add a new line without refreshing the page. Thus, for each key, there are never as many $ _POST arrays. The key that excites me (well, all of them, but as soon as I earn it, it works for everyone) is a fall $_POST['monworkhours']. This is a drop-down list with a list of different hours of work. I believe the problem is that I need to scroll through the entire array $_POST['monworkhours']based on the view. I do not know how to do that.

In addition, the “problem” produces the same results for each line of output. Therefore, everything that I set for the first field becomes the result for each additional line that I added "through" through 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 output of $ _POST (2 lines of lines):

 ["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 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 sending one line of the form, everything is in order; this is when I have more than one; then I get duplicate information.

The following example should show you what happens when I “add an engineer” (now doing two lines of lines). I will post the result after the image:

Application Image: enter image description here

: , , , , . , , , , :

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

. style="background: yellow; color:#000;".


, , , . , , . , , , , , , , . , .

getCellColor() :

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 :

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

, , .

+3
4

, , , . , , . , , , , , . , .

getCellColor() :

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;\"";
        }
}

foreach :

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

, , .

+1

. , , , .

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

$_POST ['monworkhours'], , . , , .

+1

$_POST ['monworkhours'] :

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

, , . HTML (, <option value="7am7pm" selected>7AM-7PM</option>) javascript?

javascript, , , . . , :

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

PHP, :

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

, . .

+1

, . , :

foreach($engineer as $a => $b) {             
    echo "| [[$engineer[$a]]] || ".getCellColor('mon')." | ...";
}  

, , getCellColor();

getCellColor() , .

function getCellColor($dow) {
    foreach($_POST[$dow . 'shifthours'] as $key=> $hour) {   
    ...
    }
}

, , :

  • foreach ()
  • foreach () 1
  • 1, getCellColor
  • getCellColor
  • foreach () 2
  • 2, getCellColor
  • getCellColor ()
  • ... ..

: , , . .

As a suggestion, I would like to describe things like Patrick Fisher in his answer.

<select name="aa_1"> <select name="bb_1"> <input type="hidden" name="lines[]" value="1" />
<select name="aa_2"> <select name="bb_2"> <input type="hidden" name="lines[]" value="2" />
<select name="aa_3"> <select name="bb_3"> <input type="hidden" name="lines[]" value="3" />

Thus, as soon as you submit the form, you can get each element of the form horizontally (i.e., rows), and not vertically.

foreach($_POST["lines"] as $key => $value) { 
    ${"aa_".$value} = $_POST["aa_".$value];
    ${"bb_".$value} = $_POST["bb_".$value];
}
+1
source

Source: https://habr.com/ru/post/1793735/


All Articles