random - PHP- How to do str_shuffle make different value?
Get the solution ↓↓↓PHP- How to do str_shuffle make different values??
$randomcode="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$randomcode='NT'.substr(str_shuffle($str),5,8);
echo $randomcode.'<br>';
echo $randomcode.'<br>';
result:
NT2ZCI1qdX
NT2ZCI1qdX
How to make different values?? I want to insert it into my database.
$randomcode="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$randomcode=substr(str_shuffle($str),5,8);
$stmt = $pdo->prepare("insert into AGEGroup (AGE,hidecode) values (:age,$randomcode)");
foreach ($_POST['age'] as $age) {
$stmt->execute([':age'=>$age]);
}
Thank you very much
Answer
Solution:
You should generate your code inside the loop.
//list of possible characters in generated code
$charmap = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
//note using `:randomcode` instead of the direct variable
$stmt = $pdo->prepare("insert into AGEGroup (AGE) values (:age, :randomcode)");
foreach ($_POST['age'] as $age) {
//create a "random" string that is 8 characters.
//string will never repeat any characters, and can create duplicates
$code = 'NT' . substr(str_shuffle($charmap), -8);
//send variables to query on each loop
$stmt->execute([':age' => $age, ':randomcode' => $code]);
}
As I said in the comments in the code, this method probably has the tendency to create duplicate codes. It will also not contain any duplicate letters/numbers.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: http failure during parsing for
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.