| | |
random generated number integrated into a form
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
Hi there and thanks
Not thinking I did not put it in a .php page but a .html page. Thank you for your help. I am on the design side making site look pretty, I have some Basic knowledge of PHP but not enough. almostbob, if you could go further into your explanation I would be interested in looking at the way you have implemented the random number. I would like to take this opportunity to thank you both for your help. It is greatly appreciated.
John
Not thinking I did not put it in a .php page but a .html page. Thank you for your help. I am on the design side making site look pretty, I have some Basic knowledge of PHP but not enough. almostbob, if you could go further into your explanation I would be interested in looking at the way you have implemented the random number. I would like to take this opportunity to thank you both for your help. It is greatly appreciated.
John
php Syntax (Toggle Plain Text)
<?php if ($_POST['email']) { /* check whether the file is loading from the post and assign values to the variables */ $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; $email=$_POST['email']; $address=$_POST['address']; $telephone=$_POST['telephone']; $countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */ $outfile = "/cgi-bin/outputforms.csv"; /* define the file containing the form data, in this case a .csv text file containing 1 row of each form submitted */ $form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */ $fp = fopen($file, 'w'); /* open the couint file and erase the contents */ fputs ($fp, sprintf('%06d',$form_id)); /* put the new count number to the count file fclose($fp); $fo = fopen($outfile,'a+'); /* open the output file to append the new data to the end /* fputs ($fo, "$form_id,$firstname,$lastname,$email,$address,$telephone\n"); /* write the form to the output file */ fclose($fo); } else { $firstname=$lastname=$email=$address=$telephone=" "; /* blank all data fields */ $form_id='id will be generated on submission'; /* information prompt */ } ?> <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'> <div>ID= <?php echo $form_id." Please record this number for your records"; ?></div> Firstname <input type=text name='firstname' value="$firstname" /> Lastname <input type=text name='lastname' value="$lastname" /><br /> Email <input type=text name='email' value="$email" /> Address <input type=text name='address' value="$address" /><br /> telephone <input type=text name='telephone' value="$telephone" /><br /> <?php if (!S_POST['email']) {echo "<input type='submit' name='submit' value='submit' />" ;} else { echo "thank you"; } </form> <!-- normal html form -->
I delete the submit button on the post-submit form on this one so that it is not submitted over n over
the number is consecutive
the serial number is a simple file containing a six digit number,
the number is initially set to 000000 when the script is installed
the number is read, incremented and written back to the erased file after the form is validated
in this concept the validation is lousy(nonexistent), twas just to try it out
thanks edwinhermann for the code correction
the new six digit number and the input data are written to the output file and to the browser for the user to read
the output file is .csv comma separated values, the initial content of the file is a comma separated list of the field names
the data fields are written comma separated
excell reads csv as spreadsheets,
today I am doing accounts so Im excell in my head
the data could be sent to mail(), to the user and the site owner, or all of the above.
Last edited by almostbob; Sep 29th, 2009 at 1:57 pm.
Failure is not an option It's included free, you don't have to do anything to get it
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
@ AB:
Is the $file variable = $countfile? I couldn't see it referenced previously.
•
•
•
•
PHP Syntax (Toggle Plain Text)
$countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */ $outfile = "/cgi-bin/outputforms.csv"; /* define the file containing the form data, in this case a .csv text file containing 1 row of each form submitted */ $form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */
Happy Humbugging Christmas
I have used this little routine to create a unique (ascending) sequence number for registrations. These aren't random, they are very predictable. If you are using the number as a unique reference number, then this is probably what you want to do. It uses a text file so you don't need a database. It locks the file because you could have multiple users trying to do this at the same time (so it needs to be done in a serial fashion).
In other cases where "semi-unique" is close enough (the number generated is never used on its own to find the associated information), I have used the time stamp as the reference number. If you use the full ten characters, then it is unique unless two threads request a number in the same second. If you limit it to the last six characters, then it is less unique and you will eventually get some duplicates. If you want it more unique, you use microtime and a much longer number. For this type of use, the time-stamp info can also be combined with some other info (e.g. last name or part of the phone number) to make it even more unique.
PHP Syntax (Toggle Plain Text)
$fc =fopen("reg_ctr.txt","a+") or die("Error!"); flock($fc,LOCK_EX); /* lock the file */ $seq_ctr = fread($fc,10); $seq_ctr_next = $seq_ctr; @$seq_ctr_next = $seq_ctr_next+1; ftruncate($fc,0); $ret=fwrite($fc,$seq_ctr_next); fflush($fc); flock($fc,LOCK_UN); /* unlock the file */ $ret=fclose($fc);
In other cases where "semi-unique" is close enough (the number generated is never used on its own to find the associated information), I have used the time stamp as the reference number. If you use the full ten characters, then it is unique unless two threads request a number in the same second. If you limit it to the last six characters, then it is less unique and you will eventually get some duplicates. If you want it more unique, you use microtime and a much longer number. For this type of use, the time-stamp info can also be combined with some other info (e.g. last name or part of the phone number) to make it even more unique.
Last edited by chrishea; Sep 29th, 2009 at 3:05 pm.
•
•
•
•
@ AB:
Is the $file variable = $countfile? I couldn't see it referenced previously.
php Syntax (Toggle Plain Text)
$form_id = file_get_contents( $countfile ) + 1;
Last edited by almostbob; Sep 29th, 2009 at 5:02 pm.
Failure is not an option It's included free, you don't have to do anything to get it
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
Hi,
I also faces this same problem then i found solution which i posted here...
I also faces this same problem then i found solution which i posted here...
PHP Syntax (Toggle Plain Text)
$i=0; $rand_no=""; while($i<6) { $rand_no=$rand_no.rand(0,9); $i++; } echo "6 digit random no.".$rand_no; I hope this code may help you Thanks Tulsa
"Be honest"
"Confidence is everything"
"Confidence is everything"
•
•
Join Date: Sep 2009
Posts: 43
Reputation:
Solved Threads: 9
•
•
•
•
Hi,
I also faces this same problem then i found solution which i posted here...
PHP Syntax (Toggle Plain Text)
$i=0; $rand_no=""; while($i<6) { $rand_no=$rand_no.rand(0,9); $i++; } echo "6 digit random no.".$rand_no; I hope this code may help you Thanks Tulsa
php Syntax (Toggle Plain Text)
$rand_no = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9); echo $rand_no
It's a shame php doesn't have a python-like zfill() function, then you'd just need the one rand() or mt_rand() call, not 6. Perhaps I'm wrong, anyone out there know of a function? You could always write your own function, like this:
Anyway, this is all academic since the thread starter will be using a file-based counter (I assume).
Obviously, you could expand the function to have parameters like start, end etc.
PHP Syntax (Toggle Plain Text)
function random_zfill() { $int = mt_rand(0,999999); for($i=0; $i<(6-strlen($int)); $i++)$zeroes .= '0'; return $zeroes.$int; } $my_number = random_zfill();
Anyway, this is all academic since the thread starter will be using a file-based counter (I assume).
Obviously, you could expand the function to have parameters like start, end etc.
Last edited by ardav; Sep 30th, 2009 at 6:53 am.
Happy Humbugging Christmas
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
Hi there Ardav
To answer your question, I will be using the code you wrote at the top of this post, it works well and now that I have had to add another digit, I think it will be ok, any case the client is happy, and at the end of the day, that is the main thing. I need to find something to help me write PHP - A job for Christmas and the New Year. THANKS to everyone who has answered my question, and I mean that.
Kind regards John
To answer your question, I will be using the code you wrote at the top of this post, it works well and now that I have had to add another digit, I think it will be ok, any case the client is happy, and at the end of the day, that is the main thing. I need to find something to help me write PHP - A job for Christmas and the New Year. THANKS to everyone who has answered my question, and I mean that.
Kind regards John
![]() |
Similar Threads
- Drop Lowest Score (C++)
- need help in web development (JSP)
- Project Hangs (C#)
- random numbers into an array (Java)
- display the highest randomly generated number in an array. (C++)
- Random Binary number (C++)
- Implementing Random Images (Java)
- regarding the random generated (Java)
- Random number generation (C)
Other Threads in the PHP Forum
- Previous Thread: star ratings
- Next Thread: # symbol in get statement
Views: 605 | Replies: 21
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code compression cron curl database date directory display download dropdown dynamic echo email error file files folder form forms function functions google href htaccess html httppost image include insert integration ip java javascript joomla limit link login loop mail md5 menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search secure select server sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video votedown web xml youtube






