setcookie array problem

hello i have a file to add_cook like this

setcookie("item_codes[$orders]",$_POST['code']);
$orders is a counter another cookie.

and a file to show cookies
for ($i=0;$i<=$orders-1;$i++){
      print $item_codes[$i];
}

now the problem is upto 19th offset it work fine on adding 20th item it start giving error on previos offsets undefined offset 0
again adding it give error to second offset like undefined offset 1 what can be the problem. please help me as soon as possible.
LVL 1
zahidrahimAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PromethylCommented:

setcookie("item_codes".$orders,$_POST['code']);

$orders is a counter another cookie.

and a file to show cookies
for ($i=0;$i<=$orders-1;$i++){
      print $_COOKIE['item_codes'.$i];
}

Also, use print_r($_COOKIE); to debug.
0
zahidrahimAuthor Commented:
Now
Notice: Undefined index: item_codes0 in .....................

this error start coming when i try to enter 21st item in this array :(
0
PromethylCommented:
print_r($_COOKIE);

What does this say? Should we serialize the item_codes... make it an array?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

zahidrahimAuthor Commented:
it start removing previous offset entries from 0 and start adding new one.
i read from articles that we can only store 20 cookies only, can we increase this by using some method.
and i am making a shopping cart, should i use table to add orders what u suggest?

0
PromethylCommented:
Yes.

Store your information in an array called $item_codes_array.
      
setcookie('items',serialize($item_codes_array));

and a file to show cookies:
print_r(unserialize($_COOKIE[items]));

This will give you the entire array on the next page.
0
ZhaawZSoftware DeveloperCommented:
if ( !isset($_COOKIE['item_codes']) ) { $_COOKIE['item_codes'] = array(); }
setcookie('item_codes['.count($_COOKIE['item_codes']).']',$_POST['code']);

foreach ( $_COOKIE['item_codes'] as $code ) {
  echo $code, '<br/>';
}
0
PromethylCommented:
Value is a string, not mixed.

Description
bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )

Source: http://us3.php.net/manual/en/function.setcookie.php

This code won't work:

foreach ( $_COOKIE['item_codes'] as $code ) {
  echo $code, '<br/>';
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZhaawZSoftware DeveloperCommented:
Promethyl, did you try it before posting?
Why do you think it won't work? It works! It always has worked! It works same way as sending values through a form.

<?php
if ( !isset($_COOKIE['item_codes']) ) {
  $_COOKIE['item_codes'] = array();
}
if ( isset($_POST['code']) ) {
  setcookie('item_codes['.count($_COOKIE['item_codes']).']', $_POST['code']);
  $_COOKIE['item_codes'][] = $_POST['code']; //this adds new value to 'current' $_COOKIE['item_codes'] array
}

foreach ( $_COOKIE['item_codes'] as $code ) {
  echo $code, '<br/>';
}
?>
<form method="post" action="xxx.php">
  <input name="code">
</form>



Same way to make an array with form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input name="arr[]"/><br/>
  <input type="submit"/>
</form>
<?php
  if ( isset($_POST['arr']) ) { print_r($_POST['arr']); }
?>
This will make array called 'arr', not a string value called 'arr[]'
0
hujiCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Split: Promethyl & Zhaawz

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.