Generating E-pin or random Unique Number in PHP MYsql - php

I was searching for a php script which could generate a unique 10 digit number (numerical) for my project.
I know very little about Php and here is my code, please help.
Code here is to generate a Random pin and store in database table lucky_coupon_pins.
//check for generate button
if (isset($_POST['generate_pin'])) {
//generate a random number
$random_number=mt_rand();
//once the code is generated check if it exists in table , if so generate again else move on
$row_count = $db->row_count("SELECT * FROM " . config_item('cart', 'table_lucky_coupon_pins') . " WHERE lucky_pin = '" . $random_number . "'");
if($row_count>0)
{
$random_number=mt_rand();
$row_count = $db->row_count("SELECT * FROM " . config_item('cart', 'table_lucky_coupon_pins') . " WHERE lucky_pin = '" . $random_number . "'");
}
//insert to database
$values = array(
'lucky_pin' => $random_number,
'lucky_pin_value' => $_POST['lucky_pin_value']
);
$db->insert(config_item('cart', 'table_lucky_coupon_pins'), $values);
Here I am checking the generated pin for once, if it already exists another pin is generated..I do not know how far is this unique, also the generated pin is sometimes 9 digit and sometimes its 10 digit.

As stated in the manual mt_rand() takes two parameters, a min and a max value, so to get a 10 digit random value, use either:
$random_number = mt_rand(1000000000, 9999999999);
or
$random_number = mt_rand(1,9);
for ($i = 0; $i < 9; $i++) {
$random_number .= mt_rand(0,9)
}
Is it unique? As it is, it is with high probability, but not for sure. (You only check once if it already exists.) You might consider using a loop e.g.
$row_count = 1;
while ($row_count > 0) {
$random_number = mt_rand(1000000000, 9999999999); // i prefer the other option but this one is somehow shorter
$row_count = $db->row_count("SELECT * FROM " . config_item('cart', 'table_lucky_coupon_pins') . " WHERE lucky_pin = '" . $random_number . "'");
}

mt_rand() is supposed to be pretty good (or the best way to return random number using native php function), so have some faith in it. Also if you want 10 digit number only, try:
$random_number = mt_rand(1000000000, 9999999999);

Here is a simple function for generating pin codes
function GeneratePinCode($chars){
$pin="";
while ($chars!=0){
$pin .=rand(0,9);
$chars--;
}
return $pin;
}
As for the uniqueness of the pin code, you can store each pin that you generate in the database and run a check each time you generate a new one.

Related

update entire column with different values

I need to update tags column so each cell has the content like this:
2-5-1-14-5
or
3-9-14-19-23
or simmilar (five integers, in range from 1-25).
id column is not consecutive from 1-117, but anyway min id is 1 and max 117.
$arr = [];
$str = '';
$id = 1;
for ($x = 1; $x <= 25; $x++){
array_push($arr, $x);
}
while ($id < 117) {
shuffle($arr);
array_splice($arr, 5, 25);
foreach ($arr as $el){
$str .= $el . '-';
}
$str = rtrim($str,'-');
$db->query("update posts set tags = '" . $str . "' where id = " . $id);
$id += 1;
}
I'm not sure how to describe the final result, but it seems that the majority of cells are written multiple times.
Any help ?
To combine my comments into one piece of code:
$full = range(1, 25);
$id = 1;
while ($id < 117) {
shuffle($full);
$section = array_slice($full, 0, 5);
$str = implode('-',$section);
$db->query("update posts set tags = '" . $str . "' where id = " . $id);
$id += 1;
}
So the reset of $str is not needed anymore since I have inserted the implode() where it seems functional. The other bits of code could probably be improved.
Two warnings:
Using PHP variables directly in queries is not a good idea. Please use parameter binding. This particular piece of code might not be vulnerable to SQL-injection but if you do the same elsewhere it might be.
Your database doesn't seem to be normalized. This might cause trouble for you in the long run when you expand your application.

Random generator returning endless duplicates

I am trying to create a random string which will be used as a short reference number. I have spent the last couple of days trying to get this to work but it seems to get to around 32766 records and then it continues with endless duplicates. I need at minimum 200,000 variations.
The code below is a very simple mockup to explain what happens. The code should be syntaxed according to 1a-x1y2z (example) which should give a lot more results than 32k
I have a feeling it may be related to memory but not sure. Any ideas?
<?php
function createReference() {
$num = rand(1, 9);
$alpha = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 1);
$char = '0123456789abcdefghijklmnopqrstuvwxyz';
$charLength = strlen($char);
$rand = '';
for ($i = 0; $i < 6; $i++) {
$rand .= $char[rand(0, $charLength - 1)];
}
return $num . $alpha . "-" . $rand;
}
$codes = [];
for ($i = 1; $i <= 200000; $i++) {
$code = createReference();
while (in_array($code, $codes) == true) {
echo 'Duplicate: ' . $code . '<br />';
$code = createReference();
}
$codes[] = $code;
echo $i . ": " . $code . "<br />";
}
exit;
?>
UPDATE
So I am beginning to wonder if this is not something with our WAMP setup (Bitnami) as our local machine gets to exactly 1024 records before it starts duplicating. By removing 1 character from the string above (instead of 6 in the for loop I make it 5) it gets to exactly 32768 records.
I uploaded the script to our centos server and had no duplicates.
What in our enviroment could cause such a behaviour?
The code looks overly complex to me. Let's assume for the moment you really want to create n unique strings each based on a single random value (rand/mt_rand/something between INT_MIN,INT_MAX).
You can start by decoupling the generation of the random values from the encoding (there seems to be nothing in the code that makes a string dependant on any previous state - excpt for the uniqueness). Comparing integers is quite a bit faster than comparing arbitrary strings.
mt_rand() returns anything between INT_MIN and INT_MAX, using 32bit integers (could be 64bit as well, depends on how php has been compiled) that gives ~232 elements. You want to pick 200k, let's make it 400k, that's ~ a 1/10000 of the value range. It's therefore reasonable to assume everything goes well with the uniqueness...and then check at a later time. and add more values if a collision occured. Again much faster than checking in_array in each iteration of the loop.
Once you have enough values, you can encode/convert them to a format you wish. I don't know whether the <digit><character>-<something> format is mandatory but assume it is not -> base_convert()
<?php
function unqiueRandomValues($n) {
$values = array();
while( count($values) < $n ) {
for($i=count($values);$i<$n; $i++) {
$values[] = mt_rand();
}
$values = array_unique($values);
}
return $values;
}
function createReferences($n) {
return array_map(
function($e) {
return base_convert($e, 10, 36);
},
unqiueRandomValues($n)
);
}
$start = microtime(true);
$references = createReferences(400000);
$end = microtime(true);
echo count($references), ' ', count(array_unique($references)), ' ', $end-$start, ' ', $references[0];
prints e.g. 400000 400000 3.3981630802155 f3plox on my i7-4770. (The $end-$start part is constantly between 3.2 and 3.4)
Using base_convert() there can be strings like li10, which can be quite annoying to decipher if you have to manually type the string.

PHPExcel Sumif and Skip

I just want to ask if there's a function in excel/phpexcel that can SUM a range of
cells but skip to add every one cell.
Ex. Range is A1:G1 and it will only sum A1+C1+E1+G1 skipping every 1 cell.
Or how can I do that? Please take note that range is dynamic. It could be a1:g1 or further or less.
Build the formula dynamically in your PHP script:
$startCell = 'A';
$endCell = 'H';
$row = 1;
$formula = '=' . $startCell . $row;
while ($startCell++ != $endCell && $startCell++ != $endCell) {
$formula .= '+' . $startCell . $row;
}
EDIT
Note that I've also added a pure Excel formula answer to your previous question

Random number insert mySQL not working

Hey all i am created 2 random numbers like so:
$firstlink = intval(mt_rand(100, 999) . mt_rand(100, 999) . mt_rand(1, 9) . mt_rand(100, 999)); // 10 digit
$secondLink = intval(mt_rand(1000, 999) . mt_rand(1, 999) . mt_rand(10, 99) . mt_rand(100, 999));
And this is my insert code:
$result = mysql_query("INSERT INTO userAccount
(Category,Fname,LName,firstlink,secondLink,AccDate)
VALUES ( '" . $cat . "',
'" . $fname . "',
'" . $lname . "',
" . $firstlink . ",
" . $secondLink . ",
'" . date('Y-m-d g:i:s',time()). "');");
It has no errrs and it places the data into the mysql database. However, its always the same number for BOTH firstlink and secondLink no matter who i add to the database and i have no idea why its doing it!
The datatype for both rows is INT(15)
Remove intval and all will work fine.
$firstlink = mt_rand(100, 999) . mt_rand(100, 999) . mt_rand(1, 9) . mt_rand(100, 999); // 10 digit
32 bit systems have a maximum signed integer range of -2147483648 to 2147483647. With intval you got 2147483647 mostly.
You can simplify your code and improve the randomness of the code like this:
$firstlink = mt_rand(10000,99999) . mt_rand(10000,99999);
$secondLink = mt_rand(10000,99999) . mt_rand(10000,99999);
echo "INSERT INTO userAccount
(Category,Fname,LName,firstlink,secondLink,AccDate)
VALUES ( '" . $cat . "',
'" . $fname . "',
'" . $lname . "',
" . $firstlink . ",
" . $secondLink . ",
'" . date('Y-m-d g:i:s',time()). "');"
PHPFiddle: http://phpfiddle.org/main/code/6nf-wpk
This will build your random 10-digit code by making two random 5 digit codes and joining them together. Is is simpler and easier to follow with less parts making it up. It had to be done with two mt_rand()s because the maximum number possible is 2147483647. For each mt_rand() function you're using, you're preventing a 0 from being the first digit in that section of the number, because you're starting the first digit at between 1 and 9.
If you don't care about the first number being only a 1 or 2 (and never being 3-9 or 0) you can simply use
$firstlink = mt_rand(1000000000,2147483647); // random 10 digit number
$secondLink = mt_rand(1000000000,2147483647); // random 10 digit number
As general coding tips:
Be consistent with how you name variables. You have a lowercase "L" in "$firstlink" and a capital "L" in "$secondLink". PHP is case-sensitive and you'll end up using the wrong name and getting unexpected (blank) results elsewhere in your program.
Be be careful never to put any user-provided data into a SQL command without protecting against SQL Injection attacks. Use parameterized queries as a rule. See How can I prevent SQL injection in PHP? for more details and examples.

How to generate an alphanumeric incrementing id in PHP?

I have system in PHP in which I have to insert a Number which has to like
PO_ACC_00001,PO_ACC_00002,PO_ACC_00003.PO_ACC_00004 and so on
this will be inserted in Database for further reference also "PO and ACC" are dynamic prefix they could different as per requirement
Now my main concern is how can is increment the series 00001 and mantain the 5 digit series in the number?
>> $a = "PO_ACC_00001";
>> echo ++$a;
'PO_ACC_00002'
You can get the number from the string with a simple regex, then you have a simple integer.
After incrementing the number, you can easily format it with something like
$cucc=sprintf('PO_ACC_%05d', $number);
Create a helper function and a bit or error checking.
/**
* Takes in parameter of format PO_ACC_XXXXX (where XXXXX is a 5
* digit integer) and increment it by one
* #param string $po
* #return string
*/
function increment($po)
{
if (strlen($po) != 12 || substr($po, 0, 7) != 'PO_ACC_')
return 'Incorrect format error: ' . $po;
$num = substr($po, -5);
// strip leading zero
$num = ltrim($num,'0');
if (!is_numeric($num))
return 'Incorrect format error. Last 5 digits need to be an integer: ' . $po;
return ++$po;
}
echo increment('PO_ACC_00999');
Sprintf is very useful in situations like this, so I'd recommend reading more about it in the documentation.
<?php
$num_of_ids = 10000; //Number of "ids" to generate.
$i = 0; //Loop counter.
$n = 0; //"id" number piece.
$l = "PO_ACC_"; //"id" letter piece.
while ($i <= $num_of_ids) {
$id = $l . sprintf("%05d", $n); //Create "id". Sprintf pads the number to make it 4 digits.
echo $id . "<br>"; //Print out the id.
$i++; $n++; //Letters can be incremented the same as numbers.
}
?>

Categories