TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-07-2009, 02:31 PM   #1 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default 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";
  } 
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 01-07-2009, 02:36 PM   #2 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

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.
CoryMathews is offline  
Reply With Quote
Old 01-07-2009, 02:59 PM   #3 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

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
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 01-07-2009, 03:03 PM   #4 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

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?
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 01-07-2009, 04:39 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

What are you are trying to do?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 01-07-2009, 06:52 PM   #6 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

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.
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 01-07-2009, 07:47 PM   #7 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

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...
CoryMathews is offline  
Reply With Quote
The Following User Says Thank You to CoryMathews For This Useful Post:
code_junkie (01-07-2009)
Old 01-07-2009, 11:09 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Box

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;
Attached Files
File Type: sql TalkPHP_UniqueKeyGenerator.sql (924 Bytes, 133 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Creating a Simple Currency Converter with Automatic Symbols Wildhoney General 11 03-16-2010 05:22 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM


All times are GMT. The time now is 04:53 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design