Using Arrays in $_POST

Joined: 10/07/2009

A little help Please
I am a relative new comer to PHP, so don't be too hard on me

all parts of the code below works with the execption of updating the $qty['cases'] value in the "if(isset($_POST['save'])) " portion. the $qty['each'] value is updated correctly, but it also sets the value of $qty['cases'] to be the same as $qty['each'].
in show_values.php the value returned in "$_POST[$qty['cases']] " is always equal to the value returned in "$_POST[$qty['each']]."

What am I doing wrong? Any help, suggestions, comments, opinions will be appreciated!


function display_values($truck, $change){
echo "<table><form action='show_values.php' method='post'>
echo <tr>
foreach ($truck as $container=>$qty){
if ($change == true) {
       echo "<td align='center'><input type='text' name='".$qty['cases']."' value='".$qty['cases']."' ></td><td align='center'>";
       } else {
    echo "<td>case".$qty['cases'];
}

if ($change == true) {
echo "<input type='text' name='".$qty['each']."' value='".$qty['each']."' >";
    } else {
      echo "<td>each".$qty['each'];
    }

File show_values.php
if(isset($_POST['save'])) {

    foreach ($_SESSION['cart'] as $itemid=>$qty) {
echo "Cases is : ".$_POST[$qty['cases']]."<br />";
echo "Each  is : ".$_POST[$qty['each']]."<br />";
$_SESSION['cart'][$qty['name']] = array('name'=>$qty['name'], 'caseqty'=>$_POST[$qty['caseqty']],'eachqty'=>$_POST[$qty['eachqty']]);
    }
    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items'] = calculate_items($_SESSION['cart']);
    foreach ($_SESSION['cart'] as $testitem=>$qty) {
if (($qty['caseqty']+$qty['eachqty'])==0){
   unset($_SESSION['cart'][$qty['name']]);
      }
     }
  }

display_values($_SESSION['truck'],$change);

Joined: 10/18/2008
I'm not really sure what

I'm not really sure what you're trying to do. Person comes in, fills out a couple of fields, then presses submit which gets processed in show_values... Where does the session get set?

It seems like your first loop might need to be $_POST rather than $_SESSION?

<?php
// Note:
// foreach($array as $key => $value) if you're not using the $key, you can just write
// foreach($array as $value)
foreach ($_POST['cart'] as $qty) {
echo
"Cases is : ".$_POST[$qty['cases']]."<br />";
echo
"Each  is : ".$_POST[$qty['each']]."<br />";

// Your setting the session here so don't think meant to loop the session variable
 
$_SESSION['cart'][$qty['name']] = array('name'=>$qty['name'],    
                                         
'caseqty'=>$_POST[$qty['caseqty']],
                                         
'eachqty'=>$_POST[$qty['eachqty']]);
}
$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
$_SESSION['items'] = calculate_items($_SESSION['cart']);

foreach (
$_SESSION['cart'] as $qty) {
  if ((
$qty['caseqty']+$qty['eachqty'])==0){
    unset(
$_SESSION['cart'][$qty['name']]);
  } 
}
?>

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 10/07/2009
Not sure that I'm sure either

the session is set earlier as follows:

// all initial values set to 0
if (!isset($_SESSION['cart'])){
     $_SESSION['cart'] = array();
}

$_SESSION['cart'][$new] = array('name'=>$new, 'cases'=>0; 'each'=>0);

Then display_values and the operator actually enters the quantities.
Currently it all works except that the value returned for cases is always the each quantities no matter what you actually enter. The 'Show_Values.php' works exactly as desired with the one exception.

I changed the line of code as you suggested 'foreach ($_POST['cart'] as $qty) {'
and it just gave me the following error:

Warning: Invalid argument supplied for foreach() in show_values.php on line 20 that line being 'foreach($POST['cart'] as $qyt) {

It feels like the error is in the form where I am naming the input fields????

Joined: 10/18/2008
Okay, I guess I'm not sure

Okay, I guess I'm not sure what you're trying to do...

In your show_values.php dump out all the data by using:

<?php

print "<pre>";
print_r($_POST);
print
"<pre>";
?>

And that will show you everything you have to work with. You could also do the same thing with the session variable.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 10/07/2009
I now (maybe) understand

OK, thanks for the print_r($_POST) idea, it resolves a lot, but does not tell me what I need to do.
the problem is in the

echo "<td align='center'><input type='text' name='".$qty['caseqty']."' value='".$qty['caseqty']."' size='3'></td><td align='center'>";
and
"<input type='text' name='".$qty['eachqty']."' value='".$qty['eachqty']."' size='3'>";

statements in the name= part

if I initialize the session values to all '0' I get the following in the print_r($_POST) (it creates the $_POST array (key value)=0 so everything return whatever you entered last.

Array
(
    [0] => 5
    [save] => true
    [x] => 79
    [y] => 19
)

but if I initialize the session values to say '1' and '2' I get the following in the print_r($_POST) if I enter values '5' and '6' in the input fields. the $_POST array has keys (1) and (2)
Array
(
    [1] => 5
    [2] => 6
    [save] => true
    [x] => 79
    [y] => 19
)

So, when I use the name $qty['caseqty'] and $qty['eachqty'] it is using the actual values being passed to the function as the name. So, if both values are 0 then they return [0] => (what every I enter for the last field ('eachqty') the same thing would happen if I initialized all values to '1'.
So, if at any time the the values were the same anywhere (say I had 10 items and 3 of those items had the caseqty set to 3 then it would return the last value entered to every item that started with a value of 3.

I know that I'm not explaining this very well, since if I could explain it so that I could understand it, then I would guess that i could figure this out for myself. But forgive me, I just walked off the golf course and I am still adding up my score!!!

some how I have to set the $_POST array as a two dim array just like the $_SESSION['cart'] is

$_SESSION['cart'][$itemid] => array('name', 'caseqty', 'eachqty') so that the print_r($_POST) would display like

Array
(
    [1070] => Array
        (
            [name] => 1070
            [caseqty] => 1
            [eachqty] => 2
        )

)

Then I could in the save portion I would use:
foreach ($_POST as $qty) {
$_SESSION['cart'][$qty['name']] = array('name'=>$qty['name'],    
                                          'caseqty'=>$_POST[$qty['caseqty']],
                                          'eachqty'=>$_POST[$qty['eachqty']]);
}

And I would be cooking with 'gas'

Joined: 10/18/2008
Humm, if you want your POST

Humm, if you want your POST to be a multi-dim array, you could do something like:

<?php
foreach ($truck as $container=>$qty){
if (
$change == true) {
  echo
"<td align='center'><input type='text' name='".$container.'['.$qty['cases']."]' value='".$qty['cases']."' ></td><td align='center'>";
} else {
  echo
"<td>case".$qty['cases'];
}

if (
$change == true) {
  echo
"<input type='text' name='".$container.'['.$qty['each']."]' value='".$qty['each']."' >";
} else {
  echo
"<td>each".$qty['each'];
}

?>

Then you're using your foreach key to create the array.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 10/07/2009
Thanks

Well, as Hillary says 'It takes a village to raise an idiot!' and my Mother thanks you for helping her raise her's!!

it works

Entering info into the input fields

echo "<td align='center'><input type='text' name='".$itemid.'['."caseqty"."]' value='".$qty['caseqty']."' size='3'></td><td align='center'>";
echo "<input type='text' name='".$itemid.'['."eachqty"."]' value='".$qty['eachqty']."' size='3'>";

the above returns the following in the print_r($_POST)
Array
(
    [1070] => Array
        (
            [caseqty] => 0
            [eachqty] => 5
        )

    [500] => Array
        (
            [caseqty] => 1
            [eachqty] => 0
        )

    [save] => true
    [x] => 55
    [y] => 23
)

and on the return side placing the entered values into $_SESSION

foreach ($_SESSION['cart'] as $itemid=>$qty) {
    $itemname=$qty['name'];
    $_SESSION['cart'][$itemname] = array('name'=>$itemname,
                                         'caseqty'=>$_POST[$itemname]['caseqty'],
                                         'eachqty'=>$_POST[$itemname]['eachqty']);
}

Thanks a bunch on helping me get this figured out. I learned something with every reponse that you sent!!