2015-07-19 100 views
0

如何將新陣列添加到用戶會話或相同/現有會話中。我有以下兩個PHP文件。如何在PHP中添加新陣列

  1. 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> 
    ?> 
    
  2. 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 

如何在每次用戶提交確認訂單按鈕而不覆蓋第一個訂單確認或數組時,讓數組看起來像上面那樣。

+8

你的第一個php文件應該會給你帶來很多錯誤。 – Rizier123

+2

'fruits.php'不是PHP。它是PHP標籤之間的HTML,如果您在啓用錯誤報告的情況下運行它,它將會失敗壯觀。 – 2015-07-19 01:34:25

+0

對不起,第一個文件是一個帶有php擴展名的HTML文件。主要原因是我必須在HTML標籤之前使用session_start()和另一個包含文件(php)。我沒有直截了當地想。 – Harris

回答

0

我想你想要這樣的:

<?php 
session_start(); 

if($_SERVER["REQUEST_METHOD"]=="POST") { 
    $_SESSION["data"]=array_merge($_POST['myfruit_list'],$_SESSION["data"]); 
    $data = array(); 
    $data = $_SESSION["data"]; 
    $t=2; 
    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> 

更新的要求......

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

ok..one更form..add此代碼相同的頁面.. 。

<form action="add.php" method="POST"> 
    <input type="submit" value="add"/> 
    </form> 

add.php ........

<?php 

    session_start(); 

    if($_SERVER["REQUEST_METHOD"]=="POST") 
    { 
    [email protected]_connect("localhost","root","") or die("could not connect to database"); 
    [email protected]_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++; 
    } 

    } 
    ?> 
+0

非常感謝。它像一個魅力。 – Harris

+0

爲了讓這個過程更上一層樓,如果用戶希望從數組中移除一對特定的水果,我如何移除水果數組? – Harris

+0

我已經更新了答案.....請讓我知道如果我的解決方案幫助你..... –