Add to an array with a form

Discussion in 'PHP' started by Greenmethod, Sep 11, 2007.

  1. #1
    I am trying to add to an array with a form, and am stuck. I can hard code the things into my array, but I am not quite sure how to add something to the bottom of my array. Here is the array itself:

    $_SESSION['items'] = array(
    			  		 array($_POST['item_desc'],$_POST['quantity'],$_POST['dollar_amt'],$_POST['cent_amt'])
    );
    PHP:
    I would like to store it as the session, and then add more items to it. Any help is appreciated!
     
    Greenmethod, Sep 11, 2007 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Danltn, Sep 11, 2007 IP
  3. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    looks like it.. thanks for your help!
     
    Greenmethod, Sep 11, 2007 IP
  4. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    My page is going through the first array correctly, but after that it only displays 'array' and then after that it's like it doesn't add to the array.. here is my code...

    <?php 
    session_start();
    if ($_SESSION['items']){
    	$item_array[] = $_SESSION['items'];
    	$quantity_array[] = $_SESSION['quantities'];
    	$dollar_amt_array[] = $_SESSION['dollar_amts'];
    	$cent_amt_array[] = $_SESSION['cent_amts'];
    	echo 'sessions are set';
    }
    
    if ($_POST['item']){
    	$item_array[] = $_POST['item'];
    	$quantity_array[] = $_POST['quantity'];
    	$dollar_amt_array[] = $_POST['dollar_amt'];
    	$cent_amt_array[] = $_POST['cent_amt'];
    	$_SESSION['items'] = $item_array;
    	$_SESSION['quantities'] = $quantity_array;
    	$_SESSION['dollar_amts'] = $dollar_amt_array;
    	$_SESSION['cent_amts'] = $cent_amt_array;
    $num_of_items = count($item_array);
    echo $num_of_items . '<br>';
    for ($row = $num_of_items-1; $row >= 0; $row--){
    	echo $item_array[$row] . '   ' . $quantity_array[$row] . '   ' . $dollar_amt_array[$row] . '.' . $cent_amt_array[$row] . '<br>';
    }
    }
    ?>
    <html>
    <form action="test.php" method="post">
    <table>
    		<tr>
    			<td> Item Description: </td>
    			<td> <input type="text" name="item"> </td>
    			<td> Quantity </td>
    			<td> <input type="text" name="quantity"> </td>
    			<td> Cost: </td>
    			<td> 
    				$<input type="text" name="dollar_amt">
    				.<input type="text" name="cent_amt">
    			</td>
    		</tr>
    </table>
    <input type="submit">
    </form>
    </body>
    
    </html> 
    PHP:
    The url is: www.munzcomputers.com/test.php
     
    Greenmethod, Sep 12, 2007 IP