Renee2008 Posted November 27, 2008 Share Posted November 27, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/ Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 We'd need to see code. Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700412 Share on other sites More sharing options...
Jahren Posted November 27, 2008 Share Posted November 27, 2008 It could be anything, we need some code to base our reply on ^^ Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700414 Share on other sites More sharing options...
Renee2008 Posted November 27, 2008 Author Share Posted November 27, 2008 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; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700416 Share on other sites More sharing options...
Jahren Posted November 27, 2008 Share Posted November 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700422 Share on other sites More sharing options...
Renee2008 Posted November 27, 2008 Author Share Posted November 27, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700432 Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 What's in $_POST['txtQty']? Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700435 Share on other sites More sharing options...
Renee2008 Posted November 27, 2008 Author Share Posted November 27, 2008 " $_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"; Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700443 Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 Don't use count() on $_POST['txtQty'] since it's not an array... Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700460 Share on other sites More sharing options...
Renee2008 Posted November 27, 2008 Author Share Posted November 27, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700474 Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 In that case, just use a foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700478 Share on other sites More sharing options...
Renee2008 Posted November 27, 2008 Author Share Posted November 27, 2008 Hi DarkWater, would you please make it clear? in which section i'd better to use foreach statement? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700483 Share on other sites More sharing options...
sasa Posted November 27, 2008 Share Posted November 27, 2008 $_POST['txtQty'] is NOT array change NAME property to txtQty[] Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700488 Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 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: Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700491 Share on other sites More sharing options...
Renee2008 Posted November 27, 2008 Author Share Posted November 27, 2008 I'm really frustrated now. I fixed the name property in the form field, but the $cartId[$i] and $productId[$i] still displayed the first number . Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700512 Share on other sites More sharing options...
Renee2008 Posted November 28, 2008 Author Share Posted November 28, 2008 Is there anybody who can help? Quote Link to comment https://forums.phpfreaks.com/topic/134519-php-array-only-display-the-first-number-of-element/#findComment-700957 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.