使用一组输入字段将多个post值存储在数组中,并将其作为列表回显

时间:2014-08-16 19:59:27

标签: php arrays list post populate

正如标题所说,我试图使用1行值集来制作帖子数据列表。

基本上我有13个输入字段,包含codenr,artnr,productname,count,price等。 所以当数据被发布时,它们将被存储在数组中(多维?)。然后回复并通过电子邮件发送给我。

我曾尝试使用foreach()函数来浏览已发布的值,但我对如何填充数组并列出其列表毫无头绪? 任何建议将不胜感激。

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>

这让我只是&#34; array()&#34; 很想知道我怎么能得到这样的列表。

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

更新

像这样改变了代码

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;
        }
    }
    ?>

现在它显示了我的结果。

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

但仍然没有填充数组只是用新的数组替换数组值。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

函数posteddata返回一个值,它是您想要用来访问函数存储的值的值。以下内容提供了对帖子变量的访问:

<!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>

$answer的分配是此与原始代码之间的重大差异。