Hello All,
I have created three functions, I hope I am getting the understand of this right, if I am not please inform me and give advice so I may try and learn.
So please take a look and see what you think would be good to change or add to make this better and simpler.
Thanks very much for the help!
Sincerely,
Christopher
function one random() Thanks to Weedpacket for a much better way of doing this.
<?php
function random($length = 5, $use_lower = true, $use_upper = true, $use_digit = true, $use_punct = false)
{
if( $length <= 0 )
{
trigger_error( 'Invalid length for random string', E_USER_WARNING );
return false;
}
$chars = '';
if($use_lower) $chars .= 'qwertyuioplkjhgfdsazxcvbnm';
if($use_upper) $chars .= 'QAZXSWEDCVFRTGBNHYUJMKIOLP';
if($use_digit) $chars .= '0147896325';
if($use_punct) $chars .= '!@#$%^&*=+?';
if($chars == '')
{
trigger_error('No character ranges to generate string from', E_USER_WARNING);
return false;
}
$string = '';
$maxchar = strlen( $chars ) - 1;
for($i = 0; $i < $length; $i++)
{
$string .= $chars[mt_rand( 0, $maxchar )];
}
return $string;
}
?>
function two check_id_exist()
<?php
function check_id_exist( $uses, $where_string )
{
switch($uses)
{
case 'user':
$table = 'access_credentials';
$feild = 'user_id';
// $where_string = 'S-' . $string;
break;
case 'record':
$table = 'record_status';
$feild = 'record_id';
// $where_string = 'R-' . $string;
break;
}
$query_s = "SELECT $feild FROM $table WHERE $feild = '$where_string' ";
$results_s = mysql_query($query_s) OR die("Sorry, unable to select record: " . mysql_error());
$found = mysql_num_rows($results_s);
if($found)
{ return true; } else { return false; }
}
?>
function three uses both one and two create_id()
<?php
function create_id( $uses )
{
switch($uses)
case 'user':
$id = 'U-';
break;
case 'record':
$id = 'R-';
break;
}
$stop = false;
while($stop == false)
{
$id .= random(9, false, false );
$check = check_id_exist( $uses, $id );
if($check != false)
{
$stop = false;
}
else
{
$stop = true;
}
}
return( $id );
}
?>
and the last is a script that is run to show that all of this works
<?php
include ( '/home/dev/www/lib/functions/random.php' );
include ( '/home/dev/www/lib/functions/check_id.php' );
include ( '/home/dev/www/lib/functions/create_id.php' );
include( '/home/dev/www/lib/db_config_ro-dev.php' );
include( '/home/dev/www/lib/db_conn-select.php' );
echo create_id( 'user' );
?>