Php 使用一组输入字段在数组中存储多个post值,并将其作为列表回显
正如title所说,我试图使用一行值来创建一个post数据列表 基本上我有13个输入字段,包含codenr、artnr、productname、count、price等。 因此,当数据发布时,它们将被存储在arraymulti-dimensional?中。然后回音并通过电子邮件发送给我 我曾尝试使用foreach函数来遍历已发布的值,但我不知道如何填充数组并列出它? 如有任何建议,将不胜感激 test.php 现在它向我展示了这样的结果Php 使用一组输入字段在数组中存储多个post值,并将其作为列表回显,php,arrays,list,post,populate,Php,Arrays,List,Post,Populate,正如title所说,我试图使用一行值来创建一个post数据列表 基本上我有13个输入字段,包含codenr、artnr、productname、count、price等。 因此,当数据发布时,它们将被存储在arraymulti-dimensional?中。然后回音并通过电子邮件发送给我 我曾尝试使用foreach函数来遍历已发布的值,但我不知道如何填充数组并列出它? 如有任何建议,将不胜感激 test.php 现在它向我展示了这样的结果 0 = tes1 1 = tes2 2 = tes3
0 = tes1 1 = tes2 2 = tes3 3 = tes4
但仍然不填充数组,只是用新的数组值替换数组值。
有什么建议吗?函数posteddata返回一个值,您要使用该值访问函数存储的值。以下内容提供了对post变量的访问:
<!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的赋值是这段代码与您的原始代码之间的最大区别。我删除了我的答案,因为它是错误的,无法帮助其他人。对不起,好吧,可惜你帮不了我。我将等待任何关于我问题的其他建议。如前所述,仍然获取数组作为输出。尝试了代码,但仍然获取最后发布的值,而不是它的列表。
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
<!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>