Jump to content

Dealing with ranges of numbers


Evolver

Recommended Posts

Hello,

 

First post and squeeky clean newb to mysql/php here.

 

MySQL 4.1 / PHP 5.2

 

I have a book i'm using for reference but i'm a little stuck on how to go about a project i have.

 

My client is printing out a bunch of numbered cards. 0000-9999 (well probably not all the way to 9999 but it's 4 digits)

 

She wants to be able to assign groups of 50 to a client as they get onboard. So client 1 would have 0000-0049 and then information would be broken down further based on that clients number range. It's possible in the future she may need to assign another ranger of 50 to the same client. So it could be that client 1 has 0000-0049 and 0150-0199 for example.

 

The best solution i have come up with is to create one table with one column for the number 0000-9999 and one column for the client ID's that will be assigned to a range of 50.

 

I can't figure out how to insert all of the rows into the database, 0000-9999. Is there a loop i can use?

 

I'll also have to be able to assign a range of 50 to a client ID through a form box, i think i may be able to figure this one out using PHP.

 

Do you think i am going about this correct? I'm open to your experienced suggestions.

 

Thanks, sorry for the newbness.

Link to comment
Share on other sites

I would do it in 3 tables,

Clients Table,

user_id | name

1        | userA

2        | userB

 

 

Cards Table

card_id | some_other_colum_for_cards.

0        | Somevalue   

1        | ..

...

9999

 

Clients_cards Table.

user_id | card_id

1        | 0

1        | 150

 

 

 

The above tables are saying that userA has a bunch of cards starting at 0 and 150.

 

In php, to insert

for( $i = 0; $i < 100000; $i++ )

{

  $query = " INSERT INTO Clients_cards ( ".$SomeUserId." , ".$SomeCardId." );

  $result = mysql_query($query);

}

 

 

for the user_id and cards_id in the other two tables, these columns should be auto increment and therefore the query would be such:

 

$query = " INSERT INTO Clients ( NULL, ".$SomeName." )

 

 

Link to comment
Share on other sites

$conn = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
for ($i = 0; $i <= 9999; $i++) {
  mysql_query("INSERT INTO table(fieldname) values('$i')");
}

 

Thanks i found what i needed, i figured it would be based off of that.

 

Do you guys normally just make a one time php script that you run to have some effect on the database?

 

Just curious how the workflow goes.

Link to comment
Share on other sites

$conn = mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
for ($i = 0; $i <= 9999; $i++) {
  mysql_query("INSERT INTO table(fieldname) values('$i')");
}

PLEASE don't use this code!  You should only issue a single INSERT statement, with multiple VALUES().

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.