I'm having some problems assigning values to a PHP array. I've read the help files and numerous articles here and I'm even more confused.
I have an HTML order form that's processed by PHP. The user enters the quantity, and, if necessary, a free text description of the item. Not all items have a free text description. The POST output is:
[_POST] => Array
(
[items_1] =>
[items_2] =>
[items_21] =>
[items_68] =>
[items_94] =>
[items_501] => 2
[txt_501] => Laserjet
[items_510] =>
[txt_510] => Item Description
[items_511] =>
[txt_511] => Item Description
[PlaceOrder] => Place Your Order
)
The value side of the items element is non-zero if the user entered a quantity on the form. The value side of the txt element can be user-entered or the default field description.
The object is to end up with an array of items which I can add to an orders database, ideally in the format "array(itemno)=count,txt" where itemno is the part to the right of the underscore, count is > 0 and txt is the value part of the txt element if there is a corresponding count. For this sample, the desired output would be items("501")="2,Laserjet"
My PHP code is:
foreach($_POST as $key => $value) {
$_POST[$key] = filter($value); //POST variables are filtered for bad input
$x = explode("_", $key);
$itemno=$x[1];
if ($x[0] = "txt") { $desc = $value; }
if ($x[0] = "items") { $count = $value; }
echo $itemno,"|", $count,"|",$desc,"|<br/>";
if (($count > 0) and ($itemno > 0)) { $items[$itemno] = $count;}
}
I have reduced this down to that last line of code, and it doesn't work.
What the heck am I doing wrong?
Changing these two lines:
if ($x[0] = "txt") { $desc = $value; }
if ($x[0] = "items") { $count = $value; }
to something like:
if ($x[0] == "txt") { $items[$itemno][1] = $value; }
elseif ($x[0] == "items") { $items[$itemno][0] = $value; }
and completely dropping that last line should make you get the items array like this:
$items[501] = array(2, 'Lasetjet');
Which is then easy to convert into 2, Laserjet
.
So, the full code would look like this:
foreach($_POST as $key => $value) {
$_POST[$key] = filter($value); //POST variables are filtered for bad input
$x = explode("_", $key);
$itemno = $x[1];
if ($x[0] == "txt") { $items[$itemno][1] = $value; }
elseif ($x[0] == "items") { $items[$itemno][0] = $value; }
}
var_dump($items); // see the whole array
UPDATE
Noticed a huge error - you were using the assignment operator (=
) instead of comparison (==
).
See the full working example here: http://ideone.com/kETOvZ
It would be better to change your HTML so that the input names are in array syntax:
Item # 1: Count: <input type="text" name="item[1]"/> Description: <input type="text" name="txt[1]"/><br/>
Item # 21: Count: <input type="text" name="item[21]"/> Description: <input type="text" name="txt[21]"/><br/>
...
PHP will then create put these into sub-arrays in $_POST
, and you can do this:
$items = array();
foreach ($_POST['item'] AS $itemno => $count) {
echo $itemno, "|", $count, "|", $_POST['txt'][$itemno], "|<br/>";
$items[$itemno] = array('count' => $count, 'txt' => $_POST['txt'][$itemno]);
}