Jump to content

[SOLVED] Is there a php feature that can do this


SirChick

Recommended Posts

Im trying find a way to do something like this:

 

firstly this bit i can do but you need to understand what im on about:

 

get all the rows.. and put the particular field values into an array lets say its just called "number".

 

Now say there is 3 records with these numbers:

 

record 1 = 1

record 2 = 10

record 3 = 4

 

Now what im trying to do is limit the amount of rows to 50. But each number must be unique but the user can pick the number so i cannot use auto increment plus the rows can be removed if the user deletes their number choice etc.

 

The important thing though is this part.. im trying to basically once those 3 numbers are in an array.. try to take out those numbers from a value of 50. But what i don't want it doing is a subtraction.

 

think of it like lottery numbers not like 50 subtract 4 subtract 10 subtract 1.

 

Then when it finds the "remaining numbers" itll use that against the user's input for their number choice.. if the number is still remaining its available to use if not then its in use.

 

Something like:

 

1 2 3 4 all the way to 50

then rid of 1 and 4 cos they are taken...and 10

 

Would i have to have one fixed array and one loading up the data from a table and then doing a comparison ? I was thinking ton's of if statements but there must be a quicker way?

Link to comment
Share on other sites

$nums = range(1,50);
unset($nums[array_search($record1, $nums)]);
unset($nums[array_search($record2, $nums)]);
unset($nums[array_search($record3, $nums)]);
print_r($nums);

 

It should print out the remaining numbers. $nums will now be an array from 1-50 without the 3 records.

 

Edit: of course, array_search is a lot of work for that. You could do a faster version like this:

$nums = range(1,50);
unset($nums[($record1-1]);
unset($nums[($record2-1]);
unset($nums[($record3-1]);
print_r($nums);

 

Since arrays start at 0.

Link to comment
Share on other sites

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.