Jump to content

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


Go to solution Solved by ginerjm,

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?

  • Solution

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)

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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