Generating a new unique alpha numeric string, - php

I need to generate an alpha numeric string and compare with database values but if the value is present in database then we should generate a different numeric string and again it should be compared with database values....
<?php
$character_array = array_merge(range('a', 'z'), range(0, 9));
$Emp_Id = "";
for($i = 0; $i < 6; $i++) {
$Emp_Id .= $character_array[rand(0, (count($character_array) - 1))];} ?>
which is working fine by creating an alpha numeric code,But as i have mentioned above i need to compare it with database values.
$chkEmp_Id=mysql_fetch_array(mysql_query("SELECT Emp_Id FROM talent_users WHERE Emp_Id='{$Emp_Id}' "));
after comparing with database values if the value is present then it should generate a different "Emp_Id",every time generated employee id should be compared with database values...hope u guys got it na?please help me to fix this...

$pool = array_merge (range ('a', 'z'), range(0, 9));
do
{
$hash = "";
for ($i = 0; $i < $lengthOfHash; ++$i)
{
$hash .= $pool[array_rand ($pool)];
}
while ( mysql_result (mysql_query ('SELECT COUNT(1) FROM talent_users WHERE Emp_Id = ' . $hash), 0) > 0);
But this is a big bottleneck when the table size grows. Since the hash is alpha-numeric, why can't the hash just begin with the userId?
//hash = <a number> + <a letter> + <alpha-numeric chars>
$hash = $userId . generateHash();
Just make sure that the generateHash() function doesn't start with a number, as stated in the comment.
It would then be something like:
$hash = $userId;
$letters = range ('a', 'z');
$pool = array_merge ($letters, range (0, 9));
for ($i = 0; $i < $lengthOfHash; ++$i)
{
$chooseFrom = $pool;
if ( ! $i )
$chooseFrom = $letters;
$hash .= $chooseFrom[array_rand($chooseFrom)];
}

Related

How to set the data can do the increment?

May I know how to set the $n variable once I insert the data, $n will plus 1 to previous my insert number?
For similar example what I've tried:
$n = "AB000";
for ($n = 0; $n <= 0; $n++)
{
$query = "INSERT INTO [tablename] (user, country, batch_number) VALUES ('$user', '$country', $n)";
// execute query
}
I want the expect result can like below the table, every time I do the insert function, the batch number can auto add 1:
user | country| batch_number
John USA AB009
Lawn Germany AB010
Shawn England AB011
Hope someone can guide me how to solve this problem. Thanks.
What you think about this script.
This is a different approch to acheive
online fiddle: http://sandbox.onlinephpfunctions.com/code/569b6458b3080b4fa5c5f3d96bd0839334b02e8b
<?php
$n = "A00";
$user = "userName";
$country = "country";
for ($i=0; $i < 10; $i++) {
$batch = "";
$batch = $n."".$i;
echo "INSERT INTO tablename (user, country, batch_number) VALUE "."('".$user."', '".$country."', $batch)";
echo "\n";
}
?>
You could create a get_batch_number function which needs:
$number (in your case $n)
$prefix (in your case 'AB')
$pad_length (in your case 3)
for ($n = 0; $n <= $max_n; $n++) {
$batch_number = get_batch_number($n, 'AB', 3);
// ...
}
function get_batch_number($number, $prefix, $pad_length) {
return $prefix . str_pad($number, $pad_length, '0', STR_PAD_LEFT);
}

Generate an integer bigger than 2,147,483,647 in PHP

I'm trying to make an API for my C# project in PHP to store users, but my problem is that my u_id (user id) doesn't go above 2,147,483,647. I get the error "duplicate found..." in SQL because I set it as unique, and all id's becomes 2,147,483,647 because it doesn't go above it. I've tried to force it into appending each number as a string onto a variable but that doesn't seem to work. (there are no errors with the code)
<?php
class f {
public static function randInt($length = 18) {
$output = array(rand(1, 9));
$return = "";
for($i = 0; $i < $length; $i++) {
$output[] = (string)mt_rand(0, 9);
}
for($j = 0; $j < count($output); $j++) {
$return .= (string)$output[$j];
}
return $return;
}
}
?>
Simply change column type INT to BIGINT.
ALTER TABLE tablename MODIFY columnname BIGINT;

PHPEXCEL : how to merge cells dynamically based on count

I want to merge cells dynamically based on count using PHPEXCEl.
For example:
if $count = 2;
I want to merge two cells as given below,
$objPHPExcel->getActiveSheet()->mergeCells('A1:B1');
similarly, if $count = 4;
$objPHPExcel->getActiveSheet()->mergeCells('C1:F1');
similarly, if $count = 5;
$objPHPExcel->getActiveSheet()->mergeCells('G1:K1');
I want to get this logic in a loop.
I tried the below logic, which doesn't work
$count = ew_Execute("SELECT COUNT(*) FROM ems_defects_codes WHERE DEF_CODE = '$def_code'");
$start_letter = A;
$rowno = 1;
for ($i = 0; $i < $count ; $i++) {
$objPHPExcel->getActiveSheet()->mergeCells($start_letter.$rowno.':'.$i.$rowno);
}
Any help will be much appreciated.Thanks..!!
You need to get column range string value for the inputs - start_letter, row_number and count. Once the column range is available, same can be used in the PHPExcel mergeCells function. Here is example code to get column range:
function getColRange($start_letter, $row_number, $count) {
$alphabets = range('A', 'Z');
$start_idx = array_search(
$start_letter,
$alphabets
);
return sprintf(
"%s%s:%s%s",
$start_letter,
$row_number,
$alphabets[$start_idx + $count],
$row_number
);
}
print getColRange('A', 1, 2) . PHP_EOL;
print getColRange('C', 1, 4) . PHP_EOL;
print getColRange('G', 1, 4) . PHP_EOL;
Output
A1:C1
C1:G1
G1:K1
Further you can use this new function with your code to do actual merge. You can choose to call this function or in a loop.
$sheet = $objPHPExcel->getActiveSheet();
$sheet->mergeCells(
getColRange(
$start_letter,
$row_number,
$count
)
);
The problem is that your $i inside of your loop is always going to be an integer; you need to convert that integer to the corresponding index of the alphabet, by creating an alphabetic array. This can be done with a simple range('A', 'Z').
You also need to wrap the A in $start_letter in apostrophes (as 'A'), and now that the range has been created, you can simply use the index of the alphabet for that:$start_letter = 0 (later becoming 'A' with $alphabet[$start_letter]).
Then you'll need to add the starting letter to the count for in order to get the ending cell in mergeCells(). Your starting cell now becomes $alphabet[$start_letter] . $rowno, and your ending cell now becomes ($alphabet[$start_letter] + $alphabet[$i]) . $rowno.
This can be seen in the following:
$count = ew_Execute("SELECT COUNT(*) FROM ems_defects_codes WHERE DEF_CODE = '$def_code'");
$alphabet = range('A', 'Z');
$start_letter = 0;
$rowno = 1;
for ($i = 0; $i < $count; $i++) {
$objPHPExcel->getActiveSheet()->mergeCells($alphabet[$start_letter] . $rowno . ':' . ($alphabet[$start_letter] + $alphabet[$i]) . $rowno);
}

How to generate different random number for storage in array

I am not sure but when i print_r the array, both random generated string are the same instead of different.
$amount_of_files = 2;
$generated_file_names = array();
for($i = 0; $i < $amount_of_files; $i++){
$generated_file_names[] = substr(md5(time()), 0, 10);
}
time() returns it's value to the nearest second - your code is executing in much less time than that so the value is the same. If you want random values for each item in the array use rand() or mt_rand() instead.
You can use like this
<?php
$amount_of_files = 2;
$generated_file_names = array();
for($i = 0; $i < $amount_of_files; $i++){
$generated_file_names[] = substr(md5(rand()),0,10);
}
print_r($generated_file_names);
?>
you need microtime() php is looping soo fast 0 to 2 and time() is not changing so md5 is same and sub_str is same for all.

PHP: Generating random string with both suffix and prefix as capital letters

Hie guys i want to create a random string of numbers where there is a fixed letter B at the beginning and a set of eight integers ending with any random letter, like for example B07224081A where A and the other numbers are random. This string should be unique. How can I do this?
Do you mean something like this?
$letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$numbers = rand(10000000, 99999999);
$prefix = "B";
$sufix = $letters[rand(0, 25)];
$string = $prefix . $numbers . $sufix;
echo $string; // printed "B74099731P" in my case
The more characters - the greater chance to generate unique string.
I think that's much better method to use uniqid() since it's based on miliseconds. Uniqueness of generated string is guaranteed.
This should work for you.
$randomString = "B";
for ($i = 0; $i < 9; $i++) {
if ($i < 8) {
$randomString.=rand(0,9);
}
if ($i == 8) {
$randomString.=chr(rand(65,90));
}
}
echo $randomString;

Categories