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?