Hello all,
I am trying to take three jobs and put them in to one function that can do all three.
The function does the following jobs as of now, it is used to create a random code which can be numbers, letters, mixed
then with that its uses that for a activation code, user_ids, and record_ids.
when I run it and tell it to use all defaults except length say 10 it only shows 1 char
now if I run it with either type as user_id or record_id then it gives the error of:
Notice: Uninitialized string offset: 717339699 in /lib/random.php on line 51 and it shows 9 times as thats the length for the record or user ID
any help would be grateful!
random_test.php
<?php
include ('/home/dev/www/lib/functions/random.php');
echo 'Default = ' . random(10) . '<br />';
echo 'User Id = ' . random('number','user_id', 9) . '<br />';
echo 'Record Id = ' . random('number','record_id', 9) . '<br />';
?>
random.php
<?php
function random( $type = NULL, $uses = NULL, $length = 5 )
{
switch($type)
{
default:
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
break;
case 'numbers':
$pattern = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
break;
case 'mixed':
$pattern = "12345678901234567890123456789012345678901234567890";
break;
}
switch($uses)
{
default:
for($i=0; $i<$length; $i++)
{
$key = $pattern{mt_rand(0,35)};
}
return( $key );
break;
case 'user_id':
include '/home/dev/www/lib/db_config_ro-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
do
{
$user_id = 'U-' . sprintf("%'09d", mt_rand(1, 999999999));
// $user_id = 'U-' . $pattern{mt_rand(1, 999999999)};
$query_s = "SELECT user_id FROM access_credentials WHERE user_id = '$user_id'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
// echo $user_id .'<br />';
}
while(mysql_num_rows($result_s) != 0); // don't do again if = 0
return( $user_id );
break;
case 'record_id':
include '/home/dev/www/lib/db_config_ro-dev.php';
include '/home/dev/www/lib/db_conn-select.php';
do
{
// $record_id = 'R-' . sprintf("%'09d", mt_rand(1, 999999999));
for($i=0; $i<$length; $i++)
{
$record_id = $pattern{mt_rand(0,999999999)};
// $record_id = $pattern{mt_rand(1, 35)};
}
$query_s = "SELECT record_id FROM record_status WHERE record_id = '$record_id'";
$result_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
echo $record_id .'<br />';
}
while(mysql_num_rows($result_s) != 0); // don't do again if = 0
return( $record_id );
break;
}
// }
}
?>