Jump to content


Photo

Using an Array in a form post--Help please (SOLVED!!!) :)


  • Please log in to reply
11 replies to this topic

#1 phencesgirl

phencesgirl
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 September 2006 - 05:19 PM

Hello.  I am trying to pass a variable as an array in a form post.  I have searched the PHP manual, but I cannot find the answer in it, as I am a newbie with PHP.  So far, I have only been able to pass one value at a time for the post variable 'newquantity'.  Here is the code for that:

<?php
if (isset($_POST['update'])) { //if the quantity is being changed for an item
		//rename a common variable
		$pid = $_POST['pid'];
		
		//get the product's ordered from the cart table
		$sql = "SELECT * FROM cart WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
		$result = mysql_query($sql) or die(mysql_error());
		
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			extract($row);	
			//set the variables
			$stock = $row['stock'];
			$newquantity = $_POST['newquantity'];
			
			//first, check if the user's selected quantity is greater than the stock quantity
			if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
				$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
				mysql_query($sql) or die(mysql_error());
			} else {			
				//put the current newquantity into the cart table
			    $sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
				mysql_query($sql) or die(mysql_error());
			} //end else
		} //end while
                   } //end if

         //now let the user view their cart, if it exists.	
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
        $result = mysql_query($sql) or die(mysql_error());
        
        //***make a while loop here that will repeat the table row for each item in the wish table for this user
       while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                extract($row);
?>

<table><tr>
<form action="/shop/main_shop.php?PGNM=cart" method="post"> <!--this posts to this same script-->
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity" maxlength="5"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
<td><input type="submit" name="update" value="Change Qty" /></td>
</form>
</tr></table>

<?php 
		} //end while loop
?>

I am having trouble understanding how to pass an array over the form post.  Any help or suggestions will be greatly appreciated.  Thank you so much in advance! 


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 September 2006 - 05:28 PM

newquantity is an array, which is $_POST['newquantity']. $_POST is an array of all the POST'd data from the form.

Could you define what you mena by an array. DO you want the newquantity text field to hold an array of values, eg 1,3,5,1 each number after the comma is an item within the newquantity array?



#3 phencesgirl

phencesgirl
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 September 2006 - 05:34 PM

Ok, I see what you mean that $_POST is an array.  What I would like to do is to post all the $pid (product id's) and their corresponding quantities, from the form, at one time. This way,  I can update the cart table with new quantities for each item at one time.

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 September 2006 - 05:44 PM

Then you'll want to do is create seperate fields for each product that user has in thier basket. You give the each form field the same name, but you suffix the name with two square brackets ([]) eg pid[] newquantity[]

When you do that the data will be submit as an array. To access product 1 you use $_POST['pid][0] and $_POST['newquantity'][0] to access product 2 you use $_POST['pid][1] and $_POST['newquantity][1] etc. Remeber artrays start at zero.

You're best of creatting the form fields dynamically. How do you store each product in the users basket?

#5 phencesgirl

phencesgirl
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 September 2006 - 06:07 PM

WOW!  Thank you so much!!  I think that this is what I am looking for. 

I am storing the each product in the cart table, with the username and date and confirmed (yes or no) to distinguish between different 'shopping baskets'.

Well, I am going to try to implement your solution now, and I will reply back again with the results.  Thank you again!
:)

#6 phencesgirl

phencesgirl
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 September 2006 - 07:21 PM

ok...I don't think I've implemented this correctly.  Here is my revised code:

<?php

if (isset($_POST['update'])) { //if the quantity is being changed for an item
		
		
		//get the product's ordered from the cart table
		$sql = "SELECT * FROM cart WHERE username = '$uid' AND confirmed = 'no'";
		$result = mysql_query($sql) or die(mysql_error());
		
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			extract($row);
				
			//set the variables	
			$pid = $_POST['pid'][0];
			$newquantity = $_POST['newquantity'][0];		
			$stock = $row['stock'];
			
			foreach ($_POST as $pid => $newquantity) {

				//first, check if the user's selected quantity is greater than the stock quantity
				if ($newquantity > $stock) {
					$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
					mysql_query($sql) or die(mysql_error());
				} else {			
					//put the current newquantity into the cart table
					$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
					mysql_query($sql) or die(mysql_error());
				} //end else
			} //end foreach
		} //end while
	
	} //end foreach
         //now let the user view their cart, if it exists.	
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
        $result = mysql_query($sql) or die(mysql_error());
        
        //***make a while loop here that will repeat the table row for each item in the wish table for this user
       while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                extract($row);
?>

<table><tr>
<form action="/shop/main_shop.php?PGNM=cart" method="post"> <!--this posts to this same script-->
<input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
<?php 
		} //end while loop
?>

<td><input type="submit" name="update" value="Change Qty" /></td>
</form>
</tr></table>

I added the foreach loop to go through the array items, and update the cart table with each new quantity.  However, the script is not updating the cart table with the new quantities for each product.  Is 'foreach' the wrong way to go about this?  I am trying to allow the customer to choose the quantities that they want to order of each product, by inputting those quantities into a seperate text field for each product, and clicking on one 'update qty' button at the bottom of the page.  Any help is appreciated!  Thank you!

#7 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 13 September 2006 - 07:32 PM

well I think your problem is that you are making a new form everytime you loop thru the items in the cart. You need to put the <form> tag before the while loop then close it after the loop. The way you have it now you are creating X amount of forms for each item in the cart. The way you have it you are really only getting post data from one item in the cart.

Try your form this way
<?php         //now let the user view their cart, if it exists.
?>
<form action="/shop/main_shop.php?PGNM=cart" method="POST"> <!--this posts to this same script-->
<table>
<?php
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE usrname = '$uid' ORDER BY product_id";
        $result = mysql_query($sql) or die(mysql_error());

        //***make a while loop here that will repeat the table row for each item in the wish table for this user
       while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                extract($row);
?>
  <tr>
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity" maxlength="5" value="<?=$row['quantity']?>"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
</tr>


<?php
		} //end while loop
?>
<tr>
<td><input type="submit" name="update" value="Change Qty" /></td>
</tr>
</table>
</form>

Ray

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 September 2006 - 07:51 PM

This is untested code so it may work. But try this:
<?php

// if the quantity is being changed for an item
if (isset($_POST['update']))
{
    // get the product's ordered from the cart table
	$sql = "SELECT * FROM cart WHERE username = '$uid' AND confirmed = 'no'";
	$result = mysql_query($sql) or die(mysql_error());

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
	    extract($row);

        // set the variables

        foreach ($_POST['pid'] as $pid_key => $pid)
        {
		    $newquantity = $_POST['newquantity'][$pid_key];
		    $stock       = $row['stock'];

            // first, check if the user's selected quantity is greater than the stock quantity
			if ($newquantity > $stock)
            {
			    $sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
				mysql_query($sql) or die(mysql_error());
			}
            else
            {
			    // put the current newquantity into the cart table
				$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND confirmed = 'no' AND product_id = '$pid'";
				mysql_query($sql) or die(mysql_error());
			} // end else
		} // end foreach
	} // end while
} // end if

// now let the user view their cart, if it exists.
// Select * FROM cart WHERE username == "$uid"
$sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
$result = mysql_query($sql) or die(mysql_error());
?>
<form action="/shop/main_shop.php?PGNM=cart" method="post">
  <table>
    <tr>
<?php
//***make a while loop here that will repeat the table row for each item in the wish table for this user
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
      <td>
        <input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
        <input type="text"  size="3" name="newquantity[]" maxlength="5"/>
      </td>
<?php
} //end while loop
?>
      <td><input type="submit" name="update" value="Change Qty" /></td>
    </tr>
  </table>
</form>


#9 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 13 September 2006 - 08:18 PM

I noticed also, there is no need for the 1st query. You already have the $pid so there is not need to query the cart again. just run your foreach loop THEN sql the row to get the stock amount then update the table. With the help of wildteens foreach statement this works fine for me.

<?php
if (isset($_POST['update'])) { //if the quantity is being changed for an item
    foreach($_POST['pid'] as $pid_key => $pid){
    $newquantity = $_POST['newquantity'][$pid_key];
      //get current product stock
	  $sql = "SELECT stock FROM cart WHERE product_id = '$pid'";
		$result = mysql_query($sql) or die(mysql_error());
          $row = mysql_fetch_array($result, MYSQL_ASSOC);
            //set the variables
			$stock = $row['stock'];
            //first, check if the user's selected quantity is greater than the stock quantity
			if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
				$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND status = 'no' AND product_id = '$pid'";
				mysql_query($sql) or die(mysql_error());
			} else {
				//put the current newquantity into the cart table
			    $sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND status = 'no' AND product_id = '$pid'";
				mysql_query($sql) or die(mysql_error());
			} //end else
	} //end foreach
} //end if

         //now let the user view their cart, if it exists.
?>
<form action="/shop/main_shop.php?PGNM=cart" method="POST"> <!--this posts to this same script-->
<table>
<?php
        // Select * FROM cart WHERE username == "$uid"
        $sql = "SELECT * FROM cart WHERE username = '$uid' ORDER BY product_id";
          $result = mysql_query($sql) or die(mysql_error());
          //***make a while loop here that will repeat the table row for each item in the wish table for this user
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            extract($row);
?>
  <tr>
<input type="hidden" name="pid[]" value="<?php echo $row['item_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5" value="<?=$row['quantity']?>"/></td> <!--'newquantity'--this is the variable that I would like to pass in the form of an array, so that the quantity can be changed on multiple items, with only one click on 'Change Qty' button-->
</tr>
<?php
		} //end while loop
?>
<tr>
<td><input type="submit" name="update" value="Change Qty" /></td>
</tr>
</table>
</form>


#10 phencesgirl

phencesgirl
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 September 2006 - 08:29 PM

Thank you all for helping this newbie!  :)
Thank you both for your replies.  

I have now put the form tags outside of the while loop, and have changed my script to reflect the one from craygo.  I have two items in my test shopping cart.  As the script is now, whichever number I input as the quantity of the first item, that number is put into the cart table as the quantity of the second item in my cart.  Obviously, this is not right.   I'm not sure where to go from here...but I'll keep trying!  

Thanks again for your help!

#11 phencesgirl

phencesgirl
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 September 2006 - 08:56 PM

Ok, here is the latest code:

<?php

if (isset($_POST['update'])) { //if the quantity is being changed for an item
	
		foreach($_POST['pid'] as $pid_key => $pid){
			$newquantity = $_POST['newquantity'][$pid_key];
			//get current product stock
			$sql = "SELECT stock FROM cart WHERE product_id = '$pid'";
			$result = mysql_query($sql) or die(mysql_error());
		                $row = mysql_fetch_array($result, MYSQL_ASSOC);

					//set the variables
					$stock = $row['stock'];
					//first, check if the user's selected quantity is greater than the stock quantity
					if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
						$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND product_id = '$pid'";
						mysql_query($sql) or die(mysql_error());
					} else {
						//put the current newquantity into the cart table
						$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND product_id = '$pid'";
						mysql_query($sql) or die(mysql_error());
					} //end else
		} //end foreach

	} //end if

    // Select * FROM cart WHERE username == "$uid"
    $sql = "SELECT * FROM cart WHERE username = '$uid'";
    $result = mysql_query($sql) or die(mysql_error());
?>
<form action="/test_0706/shop/main_shop.php?PGNM=cart" method="post">
<table>
<?php
		//***make a while loop here that will repeat the table row for each item in the cart table for this user
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			extract($row);
?>
<tr><input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="submit" name="remove" value="Remove"  /></td>
<td><?php echo $row['product_id']; ?></td>
<td>$<?php echo $row['price']; ?></td>
<td><a href="<?php echo $row['url']; ?>">View Item</a></td>
<td align="center">
<?php
	//put stock quantity here
	echo $stock; 
?>
</td>
<input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5" value="<?=$row['quantity']?>"/></td>
<td align="center"><?php echo $row['quantity']; ?></td></tr>
<?php 
		} //end while loop
?>
<tr><td><input type="submit" name="update" value="Change Qty" /></td></tr>
</form>

Like I said in my last post, I have a test cart with two products in it.  I am still having the problem where the number in the input field for the first product is put into the cart table as the quantity of the second product.  The quantity of the first product is never changed, no matter what numbers I put in the first and second input fields. 

All help is appreciated! 

#12 phencesgirl

phencesgirl
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 13 September 2006 - 09:28 PM

Yes!!  I don't have that problem anymore!  However, I have had to really expand the code, and now the remove button is not working properly!  Oh well, I think I can figure that one out, as I have had similar problems before. Well, thank you all so much for your help everyone!  And here is the script as it now stands:
<?php

     if (isset($_POST['update'])) { //if the quantity is being changed for an item
	
		foreach($_POST['pid'] as $pid_key => $pid){
			$newquantity = $_POST['newquantity'][$pid_key];
			//get current product stock
			$sql = "SELECT stock FROM cart WHERE product_id = '$pid'";
			$result = mysql_query($sql) or die(mysql_error());
		    $row = mysql_fetch_array($result, MYSQL_ASSOC);
					//set the variables
					$stock = $row['stock'];
					//first, check if the user's selected quantity is greater than the stock quantity
					if ($newquantity > $stock) {  //if it is, then set the order quantity to equal the stock quantity
						$sql = "UPDATE cart SET quantity = '$stock' WHERE username = '$uid' AND product_id = '$pid'";
						mysql_query($sql) or die(mysql_error());
					} else {
						//put the current newquantity into the cart table
						$sql = "UPDATE cart SET quantity = '$newquantity' WHERE username = '$uid' AND product_id = '$pid'";
						mysql_query($sql) or die(mysql_error());
					} //end else
		} //end foreach

	} //end if

    // Select * FROM cart WHERE username == "$uid"
    $sql = "SELECT * FROM cart WHERE username = '$uid'";
    $result = mysql_query($sql) or die(mysql_error());
?>
<table><tr>
<form action="/test_0706/shop/main_shop.php?PGNM=cart" method="post">
<?php
		//***make a while loop here that will repeat the table row for each item in the cart table for this user
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			extract($row);
?>
<input type="hidden" name="pid" value="<?php echo $row['product_id']; ?>" />
<td><input type="submit" name="remove" value="Remove"  /></td></tr>
<?php
	} //end while
?>
</form>
<tr>
<form action="/test_0706/shop/main_shop.php?PGNM=cart" method="post">
<?php
		// Select * FROM cart WHERE username == "$uid"
   	 	$sql = "SELECT * FROM cart WHERE username = '$uid'";
		$result = mysql_query($sql) or die(mysql_error());
		//***make a while loop here that will repeat the table row for each item in the cart table for this user
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			extract($row);
?>

<td><?php echo $row['product_id']; ?></td>
<td>$<?php echo $row['price']; ?></td>
<td><a href="<?php echo $row['url']; ?>">View Item</a></td>
<td align="center">
<?php
	//put stock quantity here
	echo $stock; 
?>
</td>
<input type="hidden" name="pid[]" value="<?php echo $row['product_id']; ?>" />
<td><input type="text"  size="3" name="newquantity[]" maxlength="5" value="<?=$row['quantity']?>"/></td>
<td align="center"><?php echo $row['quantity']; ?></td>
</tr>
<?php
	} //end while
?>
<tr><td><input type="submit" name="update" value="Change Qty" /></td>
</form>
</tr>
</table>

Thank you again!    :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users