I need to generate a ten digits product code (for bar code generation) for a fashion store, which is a combination of:
collectionYear (this or next year), two digits, e.g. 14 or 15
collectionSeason (spring to winter) one digit, e.g. 1 for spring
productId (from 1 to 99.999 with zeros filled), e.g. 12 -> 00012
productVariant (from 1 to 99 with zero filled), e.g. 2 -> 02
The result should be something like this: 1510001202
Using php I would do this like this:
$year = str_pad($year, 2 ,'0', STR_PAD_LEFT); // for future safety, e.g. 2102 :-)
$prodId = str_pad($prodId, 5 ,'0', STR_PAD_LEFT);
$prodVar = str_pad($prodVar, 2 ,'0', STR_PAD_LEFT);
$finalCode = $year.$season.$prodId.$prodVar;
Is there a mysql syntax equiv for this like
SELECT CONCAT(collectionYear,collectionSeason,productIdproductVariant) AS prodCode
with the zero fill functions included ??
Thanks for your engagement.
Related
So I want to get number with Consecutive Numbers like below.
1234567891011121314151617181920212223..............
when i input 10 then output is 1. and so on like below.
10th digit is 1
11th digit is 0
12th digit is 1
13th digit is 1
14th digit is 1
15th digit is 2
16th digit is 1
17th digit is 3
This is my code to make Consecutive Numbers. Its work as i want expected.
<?php
$i=1;
$urut='';
$a = $_GET['button1'];
echo "Your number ", $a, "<br>";
while ($i<=$a){
$urut=$urut.''.$i;
$i++;
}
$pecah = str_split($urut,1);
echo "Urut angka ke ".$a."adalah ". $pecah[$a-1];
?>
But my case is how to get number when Consecutive up to 1 million or more.
I tried that but when set 1 millions or more its load very long time and cant show the result number
You'll probably need to do something like calculating the length of each range of numbers.
For example you know that:
1 through 9 are 1 character
10 through 99 are 2 characters
100 through 999 are 3 characters
And so on
Doing so will allow you to calculate the length of the string up to a certain number.
For example the amount of characters before 1000 can be calculated like so.
9x1 + (99 - 10) x 2 + (999 - 100) x 3
Using this you can distil a formula which allows you to calculate the value for any given number.
I generate an unique security code with this every time user login:
$code = substr(str_shuffle(str_repeat("0123456789", 4)), 0, 4);
it seems works but sometimes it generate 3 number instead of 4. also this problem occurred with rand() in past, then i decide to use str_shuffle + str_repeat.
also i insert this code in db with integer data type and length is 6.
what did i wrong or missed?
or is it a bug?
While I can't immediately say why your code sometimes returns only 3 digits, I find myself wondering why you don't create this 4-digit (call it a PIN?) code through the more numerically appropriate rand? For example, since you are going for a 4-digit PIN (between 0000 and 9999), I might write it like:
$code = rand(0, 9999);
$code = substr("000$code", -4);
That is much clearer as to its purpose (generate a random number, guarantee it's 4 digits), and less esoteric than str_repeat/str_shuffle.
EDIT (after learning $code is inserted into an integer DB field)
Why is your random string of 4 digits sometimes turning into 3 digits? Because you are inserting the value into an integer column. Either the DB or the DB Driver will attempt the moral equivalent of:
$code_to_insert = (int)$code;
at which point, if the number is less than 1000, you would get three digits.
Further, if you run your code enough times as it currently stands, you should get PIN lengths of 2 and 1 as well:
0 - 9 = ( 10 / 10000) -> 0.1% of the time
10 - 99 = ( 90 / 10000) -> 0.9% of the time
100 - 999 = ( 900 / 10000) -> 9.0% of the time
1000 - 9999 = (9000 / 10000) -> 90.0% of the time
A possible fix, given the current setup of your code and DB, might be to ensure the PIN length when you pull it out of the DB. You could use the same trick as above:
$sql = "SELECT code FROM ...";
...
$code = $row['code'];
$code = substr("000$code", -4);
Since you're storing the result in an integer field, it's not being stored as separate digits, just as a number. So it doesn't know anything about leading zeroes.
When you later retrieve the value, you can convert it to a string with leading zeroes using the str_pad function:
$code = str_pad($num, 4, '0', STR_PAD_LEFT);
The other option would be to change the datatype in the database to CHAR(4) instead of INT.
Try this:
$code = str_pad($num, 4, '0', STR_PAD_LEFT);
I need to generate an invoice number from an integer of a table with an auto incrementing ID of the database where the user purchases saved.
Example of the table invoice database:
The invoice number format floor do one of two ways.
Example 1: of the number of invoices without prefix:
0000001 |
0000002 |
0000003 |
0000004 |
0000005
Example 2: the number of invoices with prefixes:
F-0000001 |
F-0000002 |
F-0000003 |
F-0000004 |
F-0000005
Question:
1) ¿What is the best way to do this, you can do directly from MySQL or PHP?
2) ¿What is the most appropriate format Example 1 or Example 2?
I appreciate your support as always!
Thanks to Gordon Linoff, I could get a way to solve this.
I will share an example, perhaps someone may be interested.
SQL - Invoice without prefix: SELECT id, LPAD(id,7,'0') FROM invoice WHERE id = 1;
Result: 0000001
SQL - Invoice with prefix: SELECT id, CONCAT( 'F-', LPAD(id,7,'0') ) FROM invoice;
Result: F-0000001
You can write a good helper function in PHP to use it wherever you want in your application to return an invoice number. The following helper function can simplify your process.
function invoice_num ($input, $pad_len = 7, $prefix = null) {
if ($pad_len <= strlen($input))
trigger_error('<strong>$pad_len</strong> cannot be less than or equal to the length of <strong>$input</strong> to generate invoice number', E_USER_ERROR);
if (is_string($prefix))
return sprintf("%s%s", $prefix, str_pad($input, $pad_len, "0", STR_PAD_LEFT));
return str_pad($input, $pad_len, "0", STR_PAD_LEFT);
}
// Returns input with 7 zeros padded on the left
echo invoice_num(1); // Output: 0000001
// Returns input with 10 zeros padded
echo invoice_num(1, 10); // Output: 0000000001
// Returns input with prefixed F- along with 7 zeros padded
echo invoice_num(1, 7, "F-"); // Output: F-0000001
// Returns input with prefixed F- along with 10 zeros padded
echo invoice_num(1, 10, "F-"); // Output: F-0000000001
Once you are done writing the helper function, you don't need to use LPAD or CONCAT MySQL functions every time in your query to return ID with padding zeros or zeros with prefix. If you have global access to the helper function in the entire application, you only need to invoke it wherever you want to generate an invoice number.
1 - 0000001 | 0000002 | 0000003 | 0000004 | 0000005
$dbValue = 1;
echo $dbValue = str_pad($dbValue, 7, "0", STR_PAD_LEFT); // it will give 0000001;
2 - F-0000001 | F-0000002 | F-0000003 | F-0000004 | F-0000005
$dbValue = 1;
echo $dbValue = "F-".str_pad($dbValue, 7, "0", STR_PAD_LEFT); // it will produce F-0000001;
Fetch last ID from database and store it in a PHP variable.
For example, if last record is 100, then increment it by 1.
$last = 100; // This is fetched from database
$last++;
$invoice_number = sprintf('%07d', $last);
Finally, the answer for second question is,
$number = "F-". $number;
Ans 1):
You can do this with PHP(directly by concat or use str-pad ) as well as with MySQL( LPAD ) also
But as per my view you should do this by PHP, so that you can change it according to your requirements e.g. extend zeroes as per number of id's in DB.So that not to change SQL queries and make it heavy.
Ans 2):
You can use both formats but if you want to be more specific about particular user or any thing else, then use second format.
I think second format can give you more information about data
I want to make the calculation of personal number by date of birth.
The calculation is done in this manner:
Ex. 8 (day) +12 (month) + 1 + 9 + 7 + 1 (year) = 38 = 3 + 8 = 11 = 1 + 1 = 2
(the final number)
This final number must not be greater than nine.
So:
The first number comes is 38 greater than 9 and it should make 3 + 8
The second number comes is 11 greater than 9 it should make 1 + 1
The third number comes is 2 less than 9 so it is the final number.
Taking all these calculations should let out the number 2.
How can I get it with php calculation?
I suppose, you can split date to array. Then
$arr = array(8,12,1,9,7,1);
// sum array, split sum to array per digit untill more than 1 digit in sum
while (count($arr = str_split(array_sum($arr))) != 1) {}
echo $arr[0]; // 2
http://www.republicof3.com/how-to-generate-unique-promotion-discount-codes-in-php/
I have found a function to generate unique promotion code in PHP
But there is a big problem, if the number of codes to be generated more then the number of remain combination(all combination-length of exclude_codes_array), the function will become infinite loop.
For example: If the $characters = "0A"; the combination are "00", "0A", "A0", "AA" = 4, if the $no_of_codes >4, the loop is cannot stopped.
So I want to check the the number of codes and all remaining combination of codes before the loop, how to calculate the combination of a string?
As per what you have calculated for A0.
AA, 00 , A0, 0A
Generalizing and using basic permutation and combination it should be ,
2^{strlen($str)};
Discrete Math
available chars = 2 ("A", "0")
available spaces = 2 ("**")
chasr^spaces = 4
available chars = 2 ("A", "0")
available spaces = 3 ("***")
chars^spaces = 8
available chars = 3 ("A", "B", "0")
available spaces = 2 ("**")
chars^spaces = 9