RESOLVED Adding an extra element to an array on a text array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

RESOLVED Adding an extra element to an array on a text array

Post by Da_Elf »

I'm listing out a bunch of products from a database using a while loop and next to it I'm placing the quantity of products. I want to be able to update the quantity, so I've placed it inside of an input field. I've used an array for the input name so that i can grab all of the entries from the while loop. however i want to add more than just the quantity to the array entries.

Code: Select all

<?php while ($pro = mysql_fetch_array($querypr, MYSQL_ASSOC)) { ?>
<input type="text" name="product[]" value="<?=$pro['product_quantity']?>" required>
<?php }
This will put the quantity into the array.

What i want to do is that when the array is received via $_POST i want it to have the product id ( $pro['product_id'] ) followed by the quantity but i only want the value of the text input to show quantity. I can separate the id from the quantity later that's no problem

on the other side want to use this but naturally all that goes through is quantity right now so this wont work

Code: Select all

foreach ($_POST['product'] as $p){
    echo "The product is ".substr($p,0,3)." and the quantity is ".substr($p,3)."<br>";
    }  
someone suggested using

Code: Select all

<input type="text" name="product[$pro['product_id']][]" value="<?=$pro['product_quantity']?>" required>
but all i get is "Array"

if what im trying to do is not possible with arrays is there another way i can do this?
Last edited by Da_Elf on Tue Oct 23, 2012 10:11 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Adding an extra element to an array on a text array

Post by Christopher »

Try this syntax:

Code: Select all

<input type="text" name="product[product_quantity]" value="<?=$pro['product_quantity']?>" required/>
(#10850)
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: Adding an extra element to an array on a text array

Post by Da_Elf »

that would only give me product quantity which is the same thing if i used product[] what i want to do is add $pro['product_id'] and $pro['product_quantity'] together like if i did

Code: Select all

$both = $pro['product_id'].$pro['product_quantity']; 
yet have the text input drive the product quantity
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Adding an extra element to an array on a text array

Post by Christopher »

Then:

Code: Select all

<input type="hidden" name="product[product_id]" value="<?=$pro['product_id']?>"/>
<input type="text" name="product[product_quantity]" value="<?=$pro['product_quantity']?>" required/>
(#10850)
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: Adding an extra element to an array on a text array

Post by Da_Elf »

I thought of using a hidden input but how do i get both id and quantity in the foreach loop on the page that receives the form data?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Adding an extra element to an array on a text array

Post by Christopher »

What do you want the received data to look like?
(#10850)
Da_Elf
Forum Commoner
Posts: 81
Joined: Mon Dec 29, 2008 12:31 pm

Re: Adding an extra element to an array on a text array

Post by Da_Elf »

Thanks for the help!!!
ive got it working now. i wanted it to list each product id and its quantity.
this is what i ended up with

Code: Select all

<?php
if(isset($_POST['update'])){
foreach ($_POST['product'] as $p){
	echo $p['product_id']." is the id and ".$p['product_quantity']." is the quantity<br>";
	}
}
$sqlpr = "SELECT product_id, product_quantity FROM products WHERE order_id = '$myorder' ORDER BY product_id";
$querypr = mysql_query($sqlpr, $myCon);?>
<form name="cartform" action="cart.php" method="post">
<?php
$pad = 0;
while ($pro = mysql_fetch_array($querypr, MYSQL_ASSOC)) {?>
<input type="text" name="product[<?=$pad?>][product_quantity]" value="<?=$pro['product_quantity']?>" required><input type="hidden" name="product[<?=$pad?>][product_id]" value="<?=$pro['product_id']?>"/><br>
<?php $pad++; } ?>
<input name="update" type="submit" value="update" />
</form>
Post Reply