RE: Indexing of Array

From: Date: Tue, 14 Nov 2000 07:34:22 +0000
Subject: RE: Indexing of Array
Groups: php.general 
Request: Send a blank email to php-general+get-25152@lists.php.net to get a copy of this message
---------------------------------------------------------------------------- ----------------- Disclaimer: The information contained in this email is intended only for the use of the person(s) to whom it is addressed and may be confidential or contain legally privileged information. If you are not the intended recipient you are hereby notified that any perusal, use, distribution, copying or disclosure is strictly prohibited. If you have received this email in error please immediately advise us by return email at postmaster@normandy.com.au and delete the email document without making a copy. ---------------------------------------------------------------------------- ----------------- Mitesh, Typically its a good idea to send replies CC'd to the list so others may help. If you are using the setcookie() function i would suggest you were much better off using sessions. Your cookies allow the user to change and basically tell you what she wants, by using sessions you store the items ordered info on your server, hopfully untouched by means outside of your application. Use something like <?php session_register("mycart"); $mycart[$itemno] = 5; ?> But you should look up the session managament examples in your PHP manual. Also reading your code... setcookie("items[$total]", $id); // here $id is varible which comes from first page containing id of product // setcookie("quantity[$total]", "1"); // the another array which stores quantity of each product. $total++; // $total is variable which contain total products. setcookie("total", $total); // Doesnt look right.. you use the var $total to tell you how many items in the array items when count($items) should do the job. Since you are using ugly for(.. loops everywhere you are stuck using sequential keys.. once again this is not the easiest way of doing things. You should be using while loops as described earlier. You may temporarily fix your problems by adding a reset($myarray) statement before your for(.. loops. But rellying on $total for you number of items in your array is not ideal. I would suggest having one array. (ill call it $mycart) $mycart's key should be some sort of unique item id (like a Stock Code or serial number) So that when user X has $mycart["123-432"] = 12; you know that User X wants 12 of item 123-432. If you want to see what a user has use a while() loop as described in my previous mail. Hope this helps. mn Mark Nold markn@enspace.com <mailto:markn@enspace.com> Senior Consultant Change is inevitable, except from vending machines. -----Original Message----- From: Mitesh Patel [mailto:mitesh@viraat.com] Sent: Tuesday, November 14, 2000 1:28 PM To: Nold, Mark Subject: Re: Indexing of Array Hi This is ok when there is only one page, but when the use of array is continuous (i.e that array is used in many pages),it is creating problem. what i had done is that in one page user can select the product and that is stored in array . so when again he wants to shop another product then he may select another product, so when user wants to display what are the products what he had selected then it should show both products as he had shopped two products so what i had done is that i had setcookie for the index(key) of that array so whenever he wants to display the products i put that arrray in for loop and display all the products. This is working properly for only 9 products The scripts i had used is this in the first page all products all the products are displayed which comes from database the second page script ... <?php setcookie("items[$total]", $id); // here $id is varible which comes from first page containing id of product // setcookie("quantity[$total]", "1"); // the another array which stores quantity of each product. $total++; // $total is variable which contain total products. setcookie("total", $total); // ?> <?for displaying the items the for loop is this <?php for ($m=0;$m<$total;$m++) { $result2 = mysql_query("select * from item where model = '$items[$m]'"); while($row = mysql_fetch_array($result2)) { $itemno = $row["model"]; $itemtype = $row["cate"]; $price = $row["price"]; $subtotal = $subtotal + $total; } } ?> so when user comes for selecting the other products index (key) values of $items gets increased as as $total which is the index for $items array. the above script is working fine for only 9 products the below is not the script which i wrote this only to make easy.... $items[0] = "any id" $items[1] = "any id" $items[2] = "any id" $items[3] = "any id" $items[4] = "any id" $items[5] = "any id" $items[6] = "any id" $items[7] = "any id" $items[8] = "any id" $items[9] = "any id" Now when the index(values) increases than 9, what it is doing ??? . it removes $item[0] from array lists and again it gets increased it again removes items from the array now it is $item[1] this goes on as the indexes gets increased and two new indexes are from ... this is ok $item[10] = "any id" $item[11]="any id" now there should be in all 12 indexes but there is only 9 as i explained it starts removing indexes completely echo count($items) ; // this returns only 9 but this should be 12 echo $total; // this returns correct values as it gets increased what it is doing i unable to figure out , if there is problem with script it won't store any values in the indexes why it is creating problem ??? Do we require to do something else for storing more than 10 values in array when we using setcookie ??? Mitesh ----- Original Message ----- From: Nold, Mark < Mark.Nold@normandy.com.au <mailto:Mark.Nold@normandy.com.au> > To: 'Mitesh Patel' < mitesh@viraat.com <mailto:mitesh@viraat.com> >; < php-windows@lists.php.net <mailto:php-windows@lists.php.net> > Sent: Tuesday, November 14, 2000 7:07 AM Subject: RE: Indexing of Array ---------------------------------------------------------------------------- I doubt your problem is your array but how you are manipulating your array. Id recommend looking for "for" loops with conters such as $i++ in them .. the best way to parse an array is a "while" loop. $ary[] = "foo"; $ary["textkey"] = 1231432; $ary[12] = 1010910; $ary[] = "The next available"; $ary[] = "key can be "; $ary[] = "set with simply "; $ary[] = "plain square brackets"; while(list($key,$value) = each($ary)){ echo "<b>$key</b> = $value <br>\n"; } So you can see you dont have to worry about what your keys are. This is great because you can then set your keys to be meaningful such as a unique product id (and maybe the value being the quantity or another array) Since you are not reliing on sequential keys you can also unset($ary[12]) and not effect anything else. Good luck mn Mark Nold markn@enspace.com <mailto:markn@enspace.com> < mailto:markn@enspace.com <mailto:markn@enspace.com> > Senior Consultant Change is inevitable, except from vending machines. -----Original Message----- From: Mitesh Patel [ mailto:mitesh@viraat.com <mailto:mitesh@viraat.com> ] Sent: Saturday, November 11, 2000 12:55 PM To: php-windows@lists.php.net <mailto:php-windows@lists.php.net> Subject: Indexing of Array I am facing problem with indexing of array. In shopping cart i had used the array to store the product and for displaying, now here till certain limit it was working very fine, but when the index(key) of array in which items values where stores increased greater than 9 then it is creating problem it start changing values from the index from top of that array . it stores only 10 values 0-9, i had not defined restrictions to this array for transaction between the pages i had used cookies it is cookie problem or array ??? if any one is having any soln pls reply me ..... From Mitesh

« previous php.general (#25152) next »