TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Need help creating a function (http://www.talkphp.com/advanced-php-programming/3842-need-help-creating-function.html)

code_junkie 01-07-2009 02:31 PM

Need help creating a function
 
How can I make a function out of the code below but continue to cycle until it gets a number that does not exist.
PHP Code:

//Generate unique incident number.
  
$incident_number rand(1000000100000000);
  
//echo $incident_number;

// Check incident number.
  
$query "SELECT * FROM ers_report where incident = '$incident_number'";
  
$reslut mysql_query($query) or die(mysql_error());
  
  
$numrows mysql_num_rows($result);
  
  if (
$numrows 1) {
    
$row mysql_fetch_array($result);
    echo 
$incident_number;
  } else {
    echo 
"Failed";
  } 


CoryMathews 01-07-2009 02:36 PM

By just putting a do while loop around the entire thing will make it able to repeat. Just make the while statement while ($numrows >= 1).

On a side note why don't you just make one column in the db an auto increment field that would remove all this extra making up numbers. Also this could run for quite a few queries if you were to get a large amout of the numbers added, or run forever if all of them are added.

code_junkie 01-07-2009 02:59 PM

The number being generated is an incident report number that the user uses to refer back to there report. The client wanted it similar to say when you pay a bill online and get the payment number.

I don't understand too much about php right now, but I'll try replacing the if to a while like you said. Thank you

code_junkie 01-07-2009 03:03 PM

When I change if ($numrows < 1 ) to while ($numrows >= 1) it doesn't display the number nor does it display the rest of my form. Any thoughts?

sketchMedia 01-07-2009 04:39 PM

What are you are trying to do?

code_junkie 01-07-2009 06:52 PM

In theory I am try to generate a random number, then check the db to see if that number exist; if that number exist then to generate a new number and check to see if that one exist. Doing that process until it generates a number that does not exsit to use as a report number.

CoryMathews 01-07-2009 07:47 PM

Ye you really should look at putting another field in the db (normally the key field) and add auto increment to that. Then as a report number you can just add zeros to the front when you display it to the screen, this just makes all the numbers seem more "legit" so in the database it would be 1, 2, 3... and to the users the report number would be 00001, 00002, 00003...

Wildhoney 01-07-2009 11:09 PM

1 Attachment(s)
Taking the above ideas into consideration. How about the following? To get it working, merely import the attached SQL file and then change the MySQL details on lines 43 and 44.

php Code:
class TalkPHP_UniqueKeyGenerator
{
    public static function make()
    {
        $aChars = range('A', 'Z');
        $aRange = array_merge($aChars, range(0, 9));
       
        shuffle($aRange);
       
        $aSegments = array
        (
            implode('', array_slice($aRange, 0, 8)),
            $aChars[rand(0, count($aChars) - 1)]
        );
       
        $szKey = implode('', $aSegments);
       
        mysql_query(sprintf("INSERT INTO report (hash) VALUES ('%s')", $szKey));
        $iPrimaryKey = mysql_insert_id();
        mysql_query("UPDATE report SET hash = CONCAT(hash, last_insert_id()) WHERE id = last_insert_id()");
       
        return $szKey . $iPrimaryKey;
    }
   
    public static function get($szKey)
    {
        preg_match('~.+?(\d+)$~i', $szKey, $aMatches);
       
        if (empty($aMatches))
        {
            return false;
        }
       
        $pResult = mysql_query(sprintf("SELECT * FROM report WHERE id = %d", (int) $aMatches[1]));
        $aResult = mysql_fetch_row($pResult);
       
        return isset($aResult[0]) ? $aResult[0] : false;
    }
}
   
mysql_connect('localhost', 'Username', 'Password');
mysql_select_db('Database');

$szGeneratedKey = TalkPHP_UniqueKeyGenerator::make();
printf('Incident Report ID Set: %s<br />', $szGeneratedKey);
printf('Incident Report ID Get: %d (PK)', TalkPHP_UniqueKeyGenerator::get($szGeneratedKey));

MySQL table creation code if you don't wish to download the SQL file:

sql Code:
CREATE TABLE `report` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `hash` varchar(16) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `hash` (`hash`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;


All times are GMT. The time now is 12:54 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0