Here’s the Ruby version:
def set_confirmation_code
code = Array.new(7) { (0…9).to_a[rand(10)] }.join.to_s
if Purchase.find_by_confirmation_code(code)
return self.set_confirmation_code()
else
self.confirmation_code = code
end
end
To do this in PHP/MySQL, I would use MyActiveRecord and something like the following:
require(‘config.inc.php’);
class Order extends MyActiveRecord {
}
function set_confirmation_code(){
$code = ‘’;
foreach(range(0,7) as $digit){
$code += (string) rand(0,9);
}
if(MyActiveRecord::FindFirst(‘Order’, ‘code = "’ . $code . ‘"’)){
set_confirmation_code();
}else{
$order = MyActiveRecord::Create(‘Order’, array(‘code’ => $code));
$order->save();
return $code;
}
}
Have a look at the Github project for MAR for more details about setting up a project with that framework. GitHub - walterdavis/myactiverecord: Fork of MyActiveRecord by Jake Grimley
Walter
On Mar 22, 2012, at 7:27 PM, Walter Lee Davis wrote:
If the numbers have to be truly unique and random, then you need to store the ones you’ve used already so you don’t repeat yourself. So assume you’ll need a database. Then you make a random key, test to see if it exists already, if it does, make another one, if not, store it in the database and use it.
I’m not near the computer with that project on it, but I’ll send it later.
Walter
On Mar 22, 2012, at 6:37 PM, David Owen email@hidden wrote:
I’m not entirely sure. Just some playing around with ideas which might come to use in a project in the pipeline.
Esentially the client needs to add a unique number at the end of an link in an email which would retrieve details on the site. I’m having a re-think as it just bound to go wrong on the client side. I’m thinking the only way would be a full registration login, etc. To do anything slightly clever.
David
On 22 Mar 2012, at 22:14, Walter Lee Davis email@hidden wrote:
What are you looking for?
dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options
dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options
dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options