I tried to use rand() to make it my unique id in database. But how to make sure that this random number will not be duplicated?
<?php
$num = '';
for ($i = 0; $i < 9; $i++)
$num .= mt_rand(0, 9);
echo '<input name="counter" value="'.$num.'">';
?>
I tried to use rand() to make it my unique id in database. But how to make sure that this random number will not be duplicated?
<?php
$num = '';
for ($i = 0; $i < 9; $i++)
$num .= mt_rand(0, 9);
echo '<input name="counter" value="'.$num.'">';
?>
In case you want to insert unique values in the database table (that is how I understood you), it is better to create unique index in the database (which ensures that no duplicate entries are in table for the following column. In case of php, check that duplicate value does not already exist in your array.
<?php
$unique = array();
while( count($unique) < 9)
{
$num = mt_rand(0, 9);
if( isset($unique[$num]) == false )
$unique[$num] = true;
}
print_r($unique);
?>
It's better if you do this way.
First get the range you want with range()
Then you shuffle the array so you can get it in a random order.
Now if you want only 5, you can use array_slice.
$range = range(1, 20);
shuffle($range);
$random = array_slice($range, 0, 5);
print_r($random);
Working example: example