Php 如何向用户输入数组中的项目添加数量?
我让一个用户输入一个项目,该项目被添加到一个数组中,并显示出来。如何添加数量字段并在项目旁边显示此数量?谢谢大家!Php 如何向用户输入数组中的项目添加数量?,php,Php,我让一个用户输入一个项目,该项目被添加到一个数组中,并显示出来。如何添加数量字段并在项目旁边显示此数量?谢谢大家! <form method="post"> `Add a new item: <input type="text" name="new_name"/> <input type="submit"/> <?php $names = postVar('names') ? : array(); $names[] = postVar('new_na
<form method="post">
`Add a new item: <input type="text" name="new_name"/>
<input type="submit"/>
<?php
$names = postVar('names') ? : array();
$names[] = postVar('new_name');
foreach ($names as $name) {
echo $name . "<br/>";
echo "<input type='hidden' name='names[]' value='$name'/>";
}
echo "Current items on list";
function postVar($name) {
// only if exists
if (! isset($_POST[$name])) {
return null;
}
// fetch
$out = $_POST[$name];
// cleanup
if (is_array($out)) {
return array_map('htmlspecialchars', $out);
}
return htmlspecialchars($out);
}
?>
</form>
`添加新项目:
我知道你这样做是为了测试和学习,所以我不会详细说明。简言之:
将第2行从
`Add a new item: <input type="text" name="new_name"/>
`添加新项目:
到
`添加新项目:,件
将第7-9行从
$names[] = postVar('new_name');
foreach ($names as $name) {
echo $name . "<br/>";
$names[]=postVar('new_name');
foreach($name作为$name){
echo$name。“
”;
到
$names[postVar('new_name')]=postVar('qty');
foreach($name=>$qty){
回显$name.”,数量:“..qty.”
;
我让你了解自己-这是最好的学习方法。阅读。你可以使用会话来保持你的数量。你到底在问什么?到目前为止你做了哪些尝试?
$names[] = postVar('new_name');
foreach ($names as $name) {
echo $name . "<br/>";
$names[postVar('new_name')] = postVar('qty');
foreach ($names as $name => $qty) {
echo $name . ", qty: " . $qty . "<br/>";