Array or Cookie or

I have a project where the user can input several partnumbers in a text box one after another (max 20), they are to be displayed in a column under the input text box for review prior to being submitted to the mysql query and they must be remembered the next time the page is opened for that user, also the text box would transform into a select list so a previous entry can be selected and reposted at a later date.
I am posting back to the same page and have tried to create a Custom Array or a Cookie with the data on each partnumber post - from my research I believe that a Cookie is the way to go BUT with whatever I try the POST overwrites the last post and always displays the last partnumber posted.

All the nuts and bolts are working except this ability to build up my posted list and the tranformation of the text box to a select list.

I want to avoid storing the data in a table if possible - Is there some kind of foreach loop or something else I can setup to achieve my goal. :blush:

Thanks in advance

Show us your code. At least the form input and your attempted cookie creation.

Here’s my form -
<form name=“plus” id=“plus” method=“post” action=“”>
<textarea name=“pn” rows=“1” class=“loadIt”></textarea>
<input type=“submit” name=“addPn” id=“addPn” value=“Add”>
</form>
my php -
if((isset($_POST[‘addPn’])) && ($_POST[‘pn’] != ‘’)) {
$pn = $_POST[‘pn’];
setcookie(‘partNumbers’,$pn);
for($i = 0; $i < 20; $i++) {
$_COOKIE[‘partNumbers’] .= $pn;
}
}
I just get a string of 20 of each posted part number, 0060596665006059666500605966650060596665006059666500605966650060596665…
I’ve tried array merge and a few other things. I’m sure it can be done - I’m just a bit lost at the moment :frowning:
I’d appreciate any direction, thanks StarLion :slight_smile:

Statement #1: Be very careful using a value from a submit button. There’s a known bug in IE where hitting enter in the form submits it without passing the value contained within the submit button. It’s better to add a hidden form field and use that.

#2: Okay, so we’re trying to store individual things. Dont use a string for this.
We’re also using an untimed cookie, which means it’s equivilant to a SESSION variable. When you say “must be remembered the next time the page is opened for that user”, do you mean… the next pageload immediately afterwards? a day later? after closing the browser?

#3: Array is going to be needed. Where we store it depends on your answer to #2.

#1 Thanks for the tip on the hidden field, I have written a javascript to submit the form for the partnumbers to this initial listing by pressing the ‘+’ key on the numeric pad with the ‘enter’ key disabled, but as you say I should use a hidden field for my final submissions?
#2 The remembered part numbers should be with the user whenever they open the page again, same day, same week or … So they can use them again without typing them in again, these would be selected individually from the input text box (a bit like a select list) much like the browser remembers what you have typed in a field in the past.
#3 I knew that an array was the way to go and though of an untimed cookie being the best solution, haven’t worked with cookies before have with sessions. Keen to learn i.e. where to store it etc.
I was toying with the idea of creating a table with the username and a primary key (as you need to log into this website). The situation gets a little tricky as in - as the list of partnumbers are displayed on the screen after input the user can delete an entry (because it may be wrong, for what ever reason) So I could do that with 2 tables I suppose - this scenario could be messy that’s why I thought of using the array situation. Hope this makes scense.

Alright.

Well you certainly could do it with a table - a single one, actually, since you could just store the table as (userid,partnumber). The benefit to doing this would mean if your user changed computers, his entries would go with him. A cookie wont do this.

If you want to continue to do it with a cookie, we can, but it sounds like you still have design decisions to make :slight_smile:

That’s a good point about portability i.e. user goes to a different computer. Although in this case they are usually at their WAN based workstations and unlikely to change computers. Back to #2, further clarification - a user can enter up to 20 part numbers, delete anyone at any time before submitting to retrieve the results, delete the whole list and start again, return to the page where the list was cleared at any time in the future and have the page remember the last, up to, 20 entries.
Using tables I could copy the user’s content of the ‘entry’ table to a ‘remembered’ table as the ‘entry’ table has all of the user’s entires deleted, this would be when he user deletes all of their list. Where as individual entries are just deleted.
This would get me out of using arrays. Just had a thought - I then need to restrict the ‘remembered’ table to the last 20 part numbers - somehow…
May be using cookies is the way to go - may be less complicated.
In the meantime, I have tried a foreach loop and a for loop to try changing the index on each post and build the array that way but as you probably know this just populates the whole array index 0 through 20 with the same number each time I post. So I really need direction on building the array and displaying the past entries.
Thanks again.

Okay, so there’s some definition of operations you need to be concerned with:
1: Add part number (up to 20)
2: Delete single part number
3: Delete all part numbers
4: Uh… what? Once the list is cleared it would be gone, no?

This would get me out of using arrays.

It would get you out of using -persistant- arrays. You’ll still be using temporary arrays during the page loads.

Just had a thought - I then need to restrict the ‘remembered’ table to the last 20 part numbers - somehow…

I’m moderately sure you can do this through a semi-complex conditional query, but i’d have to defer to the gurus of SQL on that one. You could do it with a select-then-insert query pair, also.

May be using cookies is the way to go - may be less complicated.
In the meantime, I have tried a foreach loop and a for loop to try changing the index on each post and build the array that way but as you probably know this just populates the whole array index 0 through 20 with the same number each time I post. So I really need direction on building the array and displaying the past entries.
Thanks again.

So use the part number as the key of the array, not the value. You can check size with count().

I’ve tried a few ways to set the part number as the key without any success, can you steer me in the right direction? thanks.

Show me how you’ve tried to implement it.

using this post
$pN = $_POST[‘pN’];


setcookie($pN,‘partNumbers’);


for($i = 0; $i < 20; $i++) {
setcookie($pN,$i);
}


$pNum = array();
for($i=0; $i < 20;$i++ ){
$pNum[$i] = $pN;
}
As you see, one could assume I am ‘legally blind’ when it comes to working this one out. I’m sure it’s quiet easy, there’s too much fog for me at the moment (it’s a blonde moment for me right now).
I have, however, found a script from phpclasses that does the building of the cookie on each submission but I am having difficulty in modifying it to my specs as it’s written in, I believe, PDO syntax and I haven’t a clue on half of what’s going on as my knowledge of PHP is very basic and everyday stuff.

Lets take a look at this. Say your $_POST[‘pN’] is… 12.


$pN = $_POST['pN']; //$pN = 12.

setcookie($pN,'partNumbers');  //Set cookie named 12 the value 'partNumbers'.

for($i = 0; $i < 20; $i++) { //For Loop.
setcookie($pN,$i); //Set cookie named 12 the value of $1. Eventually, cookie '12' will contain 20. It will be the only value stored.
}

$pNum = array(); //Create Empty Array.
for($i=0; $i < 20;$i++ ){ //For Loop
$pNum[$i] = $pN; //Array element $i gets set the value of 12. In fact, ALL elements of $pNum will contain 12.
}

So… not quite what you were aiming for.
If you want to do it by cookie, here’s your logic to add.

(Note: Cookies cant hold arrays - they have no concept of the logic. So we need to serialize it into a string.)

blank array
[FPHP]if[/FPHP] [FPHP]isset[/FPHP] OurCookieName
[FPHP]unserialize[/FPHP] it, overwriting our blank array.
endif
if the size of the array is less than 20
append new value to array.
else
throw error
endif
[FPHP]serialize[/FPHP] the array, and store this representation in the cookie again.