Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Array $_POST loop issues
    primarykey
    data
    text
    <p>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 ... </p> <p>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 <code>$_POST['monworkhours']</code> 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 <code>$_POST['monworkhours']</code> array based on the submission. <em>I don't exactly know how to do this</em>.</p> <p>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.</p> <p>Any help is appreciated.</p> <p>The Form:</p> <pre><code>&lt;select name="monshifthours[]" id="monshifthours"&gt; &lt;option value="OFF"&gt;OFF&lt;/option&gt; &lt;optgroup label="Front/Back Half"&gt; &lt;option value="7am7pm"&gt;7AM-7PM&lt;/option&gt; &lt;option value="7pm7am"&gt;7PM-7AM&lt;/option&gt; &lt;option value="7am7pmalt"&gt;7AM-7PM (Alt)&lt;/option&gt; &lt;option value="7pm7amalt"&gt;7PM-7AM (Alt)&lt;/option&gt; &lt;/optgroup&gt; &lt;optgroup label="Monday - Friday"&gt; &lt;option value="630am330pm"&gt;630AM-330PM&lt;/option&gt; &lt;option value="7am4pm"&gt;7AM-4PM&lt;/option&gt; &lt;option value="8am5pm"&gt;8AM-5PM&lt;/option&gt; &lt;option value="10am7pm"&gt;10AM-7PM&lt;/option&gt; &lt;/optgroup&gt; &lt;/select&gt; </code></pre> <p>The $_POST output (2 form rows):</p> <pre><code> ["monshifthours"]=&gt; array(2) { [0]=&gt; string(6) "7am7pm" [1]=&gt; string(6) "7pm7am" } </code></pre> <p>Screenshot: <img src="https://i.stack.imgur.com/cmvLo.png" alt="enter image description here"></p> <p>getCellColor() Function:</p> <pre><code>function getCellColor($dow) { foreach($_POST[$dow . 'shifthours'] as $key=&gt; $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;\""; } } } </code></pre> <p>For Submission Output:</p> <pre><code>if (isset($_POST['submit'])) { echo preTableFrmt(); foreach($engineer as $a =&gt; $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] &lt;br /&gt;|-&lt;br /&gt;"; } echo postTableFrmt(); } else { echo "Waiting for data..."; } </code></pre> <p>Note: When submitting one row of the form, everything is fine; it's when I have more than one; then I get duplicate information.</p> <p>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:</p> <p>Application Image: <img src="https://i.stack.imgur.com/USbdZ.png" alt="enter image description here"></p> <p>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:</p> <pre><code>|- | [[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 </code></pre> <p>Note: The duplicates are here the <code>style="background: yellow; color:#000;"</code>.</p> <p><strong>My Overall Solution</strong><br /> 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.</p> <p>I ended up changing the <code>getCellColor()</code> function to the following:</p> <pre><code>function getCellColor($index,$dow) { if ( isset($_POST[$dow . 'shifthours'][$index]) &amp;&amp; ($_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;\""; } } </code></pre> <p>My <code>foreach</code> now looks like:</p> <pre><code>foreach($engineer as $a =&gt; $b) { echo "|-&lt;br /&gt;| [[$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])."&lt;br /&gt;"; } </code></pre> <p>Doing it this way ensures that I just tell it to select the correct array index, and proceed accordingly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload