如何将新阵列添加到用户会话或同一/现有会话。我有以下两个PHP文件。
fruits.php
<?php
<form action="processfruits.php" method="POST">
<select name="myfruit_list[]">
<option value="apples">Apples</option>
<option value="oranges">Oranges</option>
</select>
<select name="myfruit_list[]">
<option value="grapes">Grapes</option>
<option value="pears">Pears</option>
</select>
<input type="submit" value="Confirm Order">
</form>
?>
processfruits.php
<?php
$data = array();
$data = $_POST['myfruit_list'];
echo "Number of Fruit Bowls: " . count($data) . "<br />";
foreach($data as $selection => $name)
{
echo "<br>" . $selection . ": ";
$string = implode(", ", $name);
echo $string . "<br />";
}
?>
我希望在同一用户会话下为每次处理/确认实现的输出:
Number of Fruit Bowls: 4
Data 1: Apples, Grapes
Data 2: Oranges, Pears
Data 3: Apples, Pears
Data 4: Oranges, Grapes
每次用户提交“确认订单”按钮而不覆盖第一个订单确认或数组时,如何让数组看起来像上面的那个。
答案 0 :(得分:0)
我想你想要这个:
<?php
session_start();
if($_SERVER["REQUEST_METHOD"]=="POST") {
if(isset($_POST["del"]))
{
$oft=$_POST["del"];
unset($_SESSION["data"][2*$oft-1]);
unset($_SESSION["data"][(2*$oft-2)]);
}
else
{
$_SESSION["data"]=array_merge($_POST['myfruit_list'],$_SESSION["data"]);
}
$t=2;
$data=$_SESSION["data"];
echo "Number of Fruit Bowls: " . count($data)/2 . "<br />";
foreach($data as $selection => $name) {
if($t%2==0) {
echo "<br>";
echo "data : ".($t/2)." ";
}
else {
echo ",";
}
echo $name;
$t++;
}
}
else {
$_SESSION["data"]=[];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<select name="myfruit_list[]">
<option value="apples">Apples</option>
<option value="oranges">Oranges</option>
</select>
<select name="myfruit_list[]">
<option value="grapes">Grapes</option>
<option value="pears">Pears</option>
</select>
<input type="submit" value="Confirm Order">
</form>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" placeholder="row number to be deleted..." name="del">
<input type="submit" value="Delete row">
</form>
根据要求更新......
<form action="add.php" method="POST">
<input type="submit" value="add"/>
</form>
ok ..还有一个表单..将此代码添加到同一页面...
<?php
session_start();
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$conn=@mysql_connect("localhost","root","") or die("could not connect to database");
$db=@mysql_select_db("stack",$conn) or die("could not select database");
$data=$_SESSION["data"];
$temp="";$t=0;
foreach($data as $selection => $name) {
if($t%2==0) {
$temp=$name;
echo $temp;
}
else {
$id=rand(1000,9999);
$row=ceil(($t/2));
$sql="insert into st(id,row,fruit1,fruit2) values(\"$id\",\"$row\",\"$name\",\"$temp\")";
$res=mysql_query($sql,$conn) or die("could not execute ");
}
$t++;
}
}
?>
<强> add.php 强> ........
{{1}}