I am writing a small program that generates a sequential numbering array. I need to include potentially a prefix, leading zeros, a starting number, a suffix and a quantity and also reverse the generated values.
example:
P-000299-001
P-000298-001
P-000297-001
P-000296-001
P-000295-001
P-000294-001
P-000293-001
P-000292-001
P-000291-001
P-000290-001
P-000289-001
Here is my code to generate:
<?php
$quantity = "100";
$start = "200";
$leadingzeros = "000";
$prefix = "P-";
$suffix = "-001";
$i = $start + $quantity - 1;
while ($i >= $start)
{
echo $prefix . $leadingzeros . $i-- . $suffix . "<br />\n";
}
?>
The code all seem to function correctly until I increase the quantity to say 25000 numbers to be generated or if the number string gets too long and the server will just sit and spin until a "cannot be displayed error" appears in IE.
It had been suggested to abandone the array and simply write the generated sequence to a file and this is the code to generated the file it - it really works great and is really fast.
<?php
$startTime = time(); // this is just for measuring execution time
$quantity = "25000";
$start = "26000";
$length = 6;
$prefix = "P-";
$suffix = "-001";
$file = "test.txt";
$fh = fopen($file, 'w') or die("Could not open $file for writing");
for($ix = 1, $nbr = $start; $ix <= $quantity; $ix++, $nbr--)
{
fprintf($fh, $prefix.'%0'.$length.'d'.$suffix."\n", $nbr);
}
// all done, let's see how long it took:
$endTime = time();
echo "Script took about " . ($endTime - $startTime) . " seconds to run.";
?>
I am using this program to generate numbering files used in the printing industry. The sequential numbering works great.
I had to change your code slightly in order to produce a usable numbering file, I had to subtract "1" from the $start+$quantity in order to get the correct amount of numbers. For a start of 1000 and quantity of 10 you had 1010 as your first number and it should actually be 1009 to give you 10 numbers.
$fh = fopen($file, 'w') or die("Could not open $file for writing");
for($ix = 1, $nbr = $start+$quantity-1; $ix <= $quantity; $ix++, $nbr--)
P-001009-001
P-001008-001
P-001007-001
P-001006-001
P-001005-001
P-001004-001
P-001003-001
P-001002-001
P-001001-001
P-001000-001
Let's think of this as an 8.5" x 11" piece of paper going through a laser printer - if each page had one number on it the sequential number will be great.
Anyway, here is the next challenge: Let's say we have one 5.5" x 8.5" on an 8.5" x 11" sheet twice or two up. Each with its own number on the left and right side of the paper. That after printing will be cut and stacked on top of one another and produce a pile that is still in the right order. We need to take the $quantity+$start-1 and divide by 2 and then interleave the two sets of numbers and stop the numbering set at the starting number. easy huh!!!
P-001009-001
P-001004-001
P-001008-001
P-001003-001
P-001007-001
P-001002-001
P-001006-001
P-001001-001
P-001005-001
P-001000-001
Here is the code I have for an example:
<?php
$quantity = "10";
$start = "1000";
$leadingzeros = "00";
$prefix = "P-";
$suffix = "-001";
$i = $start + $quantity - 1;
$quantity2 = $quantity / 2;
$j = (int)($i - $quantity2);
while ($j >= $start)
{
echo $prefix . $leadingzeros . $i-- . $suffix ."<br />\n" . $prefix . $leadingzeros . $j-- . $suffix . "<br />\n";
}
?>
The third option is two completely different 5.5" x 8.5" items and two different numbering schemes on an 8.5" x 11" piece of paper.
Here is the code I have been using for it.
<?php
$quantity = "10";
$start = "1000";
$leadingzeros = "00";
$prefix = "P-";
$suffix = "-001";
$quantity2 = "10";
$start2 = "2000";
$leadingzeros2 = "000";
$prefix2 = "Q-";
$suffix2 = "-005";
$i = $start + $quantity - 1;
$k = $start2 + $quantity2 - 1;
while ($i >= $start)
{
echo $prefix . $leadingzeros . $i-- . $suffix . "<br />\n" .$prefix2. $leadingzeros2 . $k-- . $suffix2 . "<br />\n";
}
?>
Which produces:
P-001009-001
Q-0002009-005
P-001008-001
Q-0002008-005
P-001007-001
Q-0002007-005
P-001006-001
Q-0002006-005
P-001005-001
Q-0002005-005
P-001004-001
Q-0002004-005
P-001003-001
Q-0002003-005
P-001002-001
Q-0002002-005
P-001001-001
Q-0002001-005
P-001000-001
Q-0002000-005
Thanks in advance