0

So as title says, I'm trying to make a list of post data using 1 rowset of values.

Basically I have 13 input fields containing codenr,artnr,productname,count,price,etc. so when data would be posted they will be stored in array(multi dimensional?).Then echoed out and emailed to me.

I have tried to use foreach() function to go through posted values, but I'm clueless how I would populate the array and make list of it? Any suggestions would be appreciated.

test.php

<?php
class postgrab{
 public $posts = array(),$result = array();  
 public function posteddata($posts){
  foreach($posts as $datavalue){
  $result[] = $datavalue;
  }
  return $result;
}
}
?>

<html>
<body>
  <form action="" method="post">
  <input type="text" name="codenr">
  <input type="text" name="artnr">
  <input type="text" name="productname">
  <input type="text" name="price">
  <input type="submit" name="add item">
</form>
<?php
$list = new postgrab();
$list->posteddata($posts = array($_POST['codenr'],$_POST['artnr'],$_POST['productname'],$_POST['price']));
print_r($list->result);
?>
</body>
</html>

This gives me just " array() " Would love to know how i could get list something like this.

1)codenr = 123 artnr = 254 productname=apple count = 10 price = 0.50<br>
2)codenr = 321 artnr = 256 productname=pear count = 15 price = 0.69

update

Changed code like this

class postgrab{

     public $posts = array(),$result = array();  

        public function posteddata($posts){
            foreach($posts as $datavalue => $value){
             $this->result[] = array($datavalue => $value);
            }
        return $this->result;
        }
    }

    $list = new postgrab();
    $list->posteddata($posts = array($_POST['codenr'],$_POST['artnr'],$_POST['productname'],$_POST['price']));
    foreach($list->result as $items){
        foreach($items as $item => $itemvalue){
        echo $item.'='.$itemvalue;
        }
    }
    ?>

now its shows me results like so.

   0 = tes1 1 = tes2 2 = tes3 3 = tes4

but still does not populate array just replace array values with new ones. Any suggestions?

1 Answers1

0

The function posteddata returns a value, and it is that value you want to use to get access to the values stored by the function. The following gives access to the post variables:

<!DOCTYPE html>
<html>
<?php
class postgrab{
 public $posts = array(),$result = array();  
 public function posteddata($posts){
    foreach($posts as $datavalue){
       $result[] = $datavalue;
    }
    return $result;
  }
}
?>

<html>
<body>
  <form method="post">
  <input type="text" name="codenr">
  <input type="text" name="artnr">
  <input type="text" name="productname">
  <input type="text" name="price">
  <input type="submit" name="add item">
</form>
<?php
$list = new postgrab();
$answer=$list->posteddata($posts = array($_POST['codenr'],$_POST['artnr'],$_POST['productname'],$_POST['price']));
var_dump ($answer);
?>
</body>
</html>

The assignment to $answer is the big difference between this and your original code.

Bob Brown
  • 1,463
  • 1
  • 12
  • 25