0

In a form within a sqlsrv_fetch_array I have

<select class="styled-select" name="StockArray['.$row1['ProductID'].']" required><option selected></option><option>0</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option></select>

and on the page after submitting that form I have this foreach to construct an insert.

$InsertDate = date("Y-m-d H:i:s");

foreach ($_POST['StockArray'] as $ProductID => $Qty) {$InsertList .= "('".$ProductID."', '".$Qty."', '".$InsertDate."', '1'),";}
$InsertList = rtrim($InsertList, ',');

What I would like to pass is an extra variable within $_POST['StockArray'] called $row1['LastQty'] so I can add an additional column in my insert. I don't think that is technically possible for an array to have more than just one value per key but I'm here to see if there is a workaround for that for what I'm trying to do.

2
  • StockArray[<?=$row1['ProductID']?>] maybe Commented Sep 16, 2016 at 16:08
  • @devpro I'm not sure what I'm looking at, I still want product ID in there however I would also like to pass $row1['LastQty'] as well for example Commented Sep 16, 2016 at 16:19

1 Answer 1

1

You can by encoding/decoding the variables. So, for example if your productID is 4 and you want to encode a LocationID (let say 28) then you can name the variable like this: P4L28, so in php:

$sel = '<select class="styled-select" name="' . "P{$row1['ProductID']}L{$row1['LocationID']}" // and so on...

Then you will need to look into the $_POST for each combination:

foreach ($_POST as $code => $qty) {
    echo "$code => $qty <br>" ;    
}

If you just need to output the lastQty in the form then you can just print it as plain HTML, because you won't need it back in the $_POST.

<select ...><p>Last quantity: <?php echo $row1['lastQty'] ; ?></p>


    . 
Sign up to request clarification or add additional context in comments.

3 Comments

Great, I made a few tweaks but that works well for what I wanted thank you, in the form: name="StockArray['."{$row1['ProductID']};;{$row1['LastQty']}".']" and in the resulting for each: foreach ($_POST['StockArray'] as $variables => $Qty) {list($ProductID, $LastQty) = explode(";;", $variables); echo $ProductID.' '.$Qty.' '.$LastQty.'<br>';}
Interesting, I didn't think about using explode(), thanks for the feedback.
No problem, I can't claim it as an orginal thought just the path I found when looking for a way to seperate the values on the other side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.