Jump to content

Using an array and inserting ids from a mysql table until the array has 6 items


brown2005

Recommended Posts

Hi, I want to have an empty array then when I load a page it will get the id of the mysql data an insert this into the array, up to a maximum of 6 times..

 

I have no ideas how to set or update arrays, so  any help will be much appreciated

$_SESSION['numbers'] = array();
array_push($_SESSION['numbers'], $id);
print_r($_SESSION['numbers']);

So I have this code, which gives me the array:

 

Array ( [0] => 9 )

 

but what I want to do is store this, so that every time it refreshes it will keep the last time plus the new number? 

So I'm gathering you are using your page to capture one id at a time and want to save them all, up to 6.

 

When you process each page submitted, add the new $id to the session var that you have created.  Check how many there are and if less than 6, repeat.

 

After six, I don't know what you want to do.

 

Do I have to write the code?

Well, you're using a session variable so each new entry should be saved from each iteration.  Of course, if you execute that line:

 

$_SESSION['numbers'] = array();

 

each time thru, then what do you think that is doing?

Tada!!

 

So only create the array when you load the page for the first time. 

 

if (!isset($_SESSION['numbers']))

    $_SESSION['numbers'] = array();

 

 

Then each time you process your $_POST and add an entry, do:

 

if (count($_SESSION['numbers']) < 6)

{

   (redisplay your page and exit)

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.