PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 5th, 2000, 02:52 PM
alexgreg's Avatar
alexgreg alexgreg is offline
Full Access
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2000
Location: London, UK
Posts: 2,019 alexgreg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 sec
Reputation Power: 15
I am working on a project that requires each article to be assigned a unique nine-digit ID number that will be stored in a mySQL database along with some other info. I know I can use the PRIMARY KEY function to make sure it's unique in mySQL but my problem lies in the fact that I don't know how to generate a random 9-digit number in PHP! Can someone please help me out here? Thanks.

--------------------
Alex
(http://www.alex-greg.co.uk)

Reply With Quote
  #2  
Old June 5th, 2000, 02:56 PM
scollo scollo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 1999
Posts: 114 scollo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 44 sec
Reputation Power: 15
Have a look at the various random number functions at:
http://www.php.net/manual/function.srand.php

Reply With Quote
  #3  
Old June 5th, 2000, 03:28 PM
alexgreg's Avatar
alexgreg alexgreg is offline
Full Access
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2000
Location: London, UK
Posts: 2,019 alexgreg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 sec
Reputation Power: 15
The point you're missing is that it always has to be nine digits long: with no zeros at the start. How do I do this?

Reply With Quote
  #4  
Old June 5th, 2000, 03:38 PM
scollo scollo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 1999
Posts: 114 scollo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 44 sec
Reputation Power: 15
Doesn't rand (100000000, 999999999) work?

Reply With Quote
  #5  
Old June 5th, 2000, 03:55 PM
alexgreg's Avatar
alexgreg alexgreg is offline
Full Access
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2000
Location: London, UK
Posts: 2,019 alexgreg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 sec
Reputation Power: 15
OK that now works, thanks, but the number stays the same even when I refresh the page: how can I get it so that the number's different each time?

Reply With Quote
  #6  
Old June 6th, 2000, 09:08 AM
Fquist Fquist is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 6 Fquist User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You should be able to find that information at the link scollo posted..

Reply With Quote
  #7  
Old June 6th, 2000, 11:16 AM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Central Florida, USA
Posts: 2,306 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 42 m 51 sec
Reputation Power: 60
Why not just generate a number based on the current date, time, & second? It also might provide a useful way of tracking items later.

Reply With Quote
  #8  
Old June 7th, 2000, 11:59 AM
alexgreg's Avatar
alexgreg alexgreg is offline
Full Access
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2000
Location: London, UK
Posts: 2,019 alexgreg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 sec
Reputation Power: 15
OK, how does that work? Don't forget you're dealing with a novice here!

Reply With Quote
  #9  
Old June 7th, 2000, 12:20 PM
scollo scollo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 1999
Posts: 114 scollo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 42 m 44 sec
Reputation Power: 15
Hi Alex,

Sorry. Didn't mean to frustrate you. You need to call the srand() function before you call the rand() function. This causes the number to be different every time.

Try this:

srand (time());
$iNum = rand (100000000, 999999999);

-- Christopher

Reply With Quote
  #10  
Old June 7th, 2000, 03:14 PM
alexgreg's Avatar
alexgreg alexgreg is offline
Full Access
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jun 2000
Location: London, UK
Posts: 2,019 alexgreg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 sec
Reputation Power: 15
Sweet, it works. Thanks guys!

[Topic closed]

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > How do I create a random number in PHP?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap