Jump to content

PHP array only display the first number of element  

  1. 1. PHP array only display the first number of element

    • option 1
      0
    • option 2
      0


Recommended Posts

I am testing some shopping cart in my local computer.But right now I am blocked by array error:I found both the array cartId and array productId only display the first numberof one element e.g. if the cartId=91,it only display "9". and if productId=53, it will only display "5" on the screen.

 

Weird is: if i'm trying to print the whole array list like "echo "$cartId";" it will display the complete the element like "91" or "53".

 

I tried to figure out the root cause but got failed. Anybody can give me some clues?

 

Really appreciate,

 

Renee

function updateCart()
{
$cartId     = $_POST['hidCartId'];
$productId  = $_POST['hidProductId'];
$itemQty    = $_POST['txtQty']; 
$numItem    = count($itemQty);
$numDeleted = 0;
$notice     = '';


for ($i = 0; $i < $numItem; $i++) {
$newQty = (int)$itemQty[$i];


	if ($newQty < 1) {

		// remove this item from shopping cart

		$test=$cartId[$i];

		deleteFromCart($cartId[$i]);	
		$numDeleted += 1;

	} else {

		// check current stock
		$sql = "SELECT pd_name, pd_qty
		        FROM tbl_product 
				WHERE pd_id = {$productId[$i]}";

		$result = dbQuery($sql);
		$row    = dbFetchAssoc($result);

// I inserted the echo statement here so that I found the $cartId[i] and $productId[i] print out the wrong result
	echo "$i; $cartId[i];$productId[$i]"; 

		if ($newQty > $row['pd_qty']) {
			// we only have this much in stock
			$newQty = $row['pd_qty'];

 

Thanks for you guys quick response. I have pasted the code in the top. By the way, following two lines of code are the form fields which will post the value "hidCartId" and "hidProductId" to the top function. Sorry i'm still a php green hand. Really appreciate.

 

<input name="hidCartId" type="text" value="<?php echo $ct_id; ?>">
  <input name="hidProductId" type="text" value="<?php echo $pd_id; ?>">

first an error in your echo sentence

 echo "$i; $cartId[i];$productId[$i]"; 

should be with $ sign

 echo "$i; $cartId[$i];$productId[$i]"; 

 

i'm not sure I understand what you are doing

$_POST['txtQty']; contains a single string

therefore using

$newQty = (int)$itemQty[$i];

mean you will only get 1 character at a time. is it really what you want?

 

Hi Jahren, sorry for my poor typo. Yes, in the echo statement, I did miss the "$" symbol.

 

And the "$newQty = (int)$itemQty[$i];" just make sure the $newQty will be an integer? And the $newQty doesn't relate to the $cartId[$i]?I dont' know why the $cartId[$i] will only display the first number of the element?

" $_POST['txtQty']" means the product quantity that customer wants to update. e.g. If one customer want to buy 4 bags instead of 1, the new "$_POST['txtQty']" will be changed to "4".

 

I just tested statement: 

echo "$cartId, $productId,$itemQty; "; 

 

On the screen, it displayed correctly "91, 53, 4". it's the correct result.

 

But when I tried to change the code to:

echo "$cartId[$i], $productId[$i],$itemQty; ";

On the screen, it only displayed "9,5,4";

Thanks DarkWater. But the "$_POST['txtQty']" is array. Because I want to allow customer to put different items in the shopping cart and they can update the quantities for different itmes at one time. Following is the statment for your refernce:

 

  <td width="75"><input name="txtQty" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">
  <input name="hidCartId" type="text" value="<?php echo $ct_id; ?>">
  <input name="hidProductId" type="text" value="<?php echo $pd_id; ?>">
  </td>

The whole for loop should really be a foreach.  And you should probably rework your form.  Instead of those hidden fields, just throw it on the txtQty[] array:  OH WAIT, I JUST NOTICED SOMETHING.  The NAME has to be txtQty[], not the id.  That's your problem I think.

 

<input name="txtQty[<?php echo $pd_id; ?>]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">

 

EDIT: sasa beat me to the txtQty[] name thing. D:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.