943,633 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1242
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 29th, 2009
0

Re: random generated number integrated into a form

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TLCJohn is offline Offline
22 posts
since Sep 2009
Sep 29th, 2009
0

Re: random generated number integrated into a form

php Syntax (Toggle Plain Text)
  1. <?php if ($_POST['email']) { /* check whether the file is loading from the post and assign values to the variables */
  2. $firstname=$_POST['firstname'];
  3. $lastname=$_POST['lastname'];
  4. $email=$_POST['email'];
  5. $address=$_POST['address'];
  6. $telephone=$_POST['telephone'];
  7. $countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */
  8. $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 */
  9. $form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */
  10. $fp = fopen($file, 'w'); /* open the couint file and erase the contents */
  11. fputs ($fp, sprintf('%06d',$form_id)); /* put the new count number to the count file
  12. fclose($fp);
  13. $fo = fopen($outfile,'a+'); /* open the output file to append the new data to the end /*
  14. fputs ($fo, "$form_id,$firstname,$lastname,$email,$address,$telephone\n"); /* write the form to the output file */
  15. fclose($fo);
  16. }
  17. else {
  18. $firstname=$lastname=$email=$address=$telephone=" "; /* blank all data fields */
  19. $form_id='id will be generated on submission'; /* information prompt */
  20. } ?>
  21. <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
  22. <div>ID= <?php echo $form_id." Please record this number for your records"; ?></div>
  23. Firstname <input type=text name='firstname' value="$firstname" />
  24. Lastname <input type=text name='lastname' value="$lastname" /><br />
  25. Email <input type=text name='email' value="$email" />
  26. Address <input type=text name='address' value="$address" /><br />
  27. telephone <input type=text name='telephone' value="$telephone" /><br />
  28. <?php if (!S_POST['email']) {echo "<input type='submit' name='submit' value='submit' />" ;} else { echo "thank you"; }
  29. </form> <!-- normal html form -->
the form submits to itself, on submission it writes the id number into the id field and displays the data entered in other fields,
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.
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Sep 29th, 2009
0

Re: random generated number integrated into a form

Hi almostbob

Thanks for that, but to be very honest, the information has flown over my head, could I have it in layman's english, sorry but, thick as thr brown stuff this end.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TLCJohn is offline Offline
22 posts
since Sep 2009
Sep 29th, 2009
0

Re: random generated number integrated into a form

@ AB:

Quote ...
PHP Syntax (Toggle Plain Text)
  1. $countfile = "/cgi-bin/countforms.bcnt"; /* define the file containing the count */
  2. $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 */
  3. $form_id = file_get_contents( $file ) + 1; /* read the last form number and increment */
Is the $file variable = $countfile? I couldn't see it referenced previously.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,671 posts
since Oct 2006
Sep 29th, 2009
0

Re: random generated number integrated into a form

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).
PHP Syntax (Toggle Plain Text)
  1. $fc =fopen("reg_ctr.txt","a+") or die("Error!");
  2. flock($fc,LOCK_EX); /* lock the file */
  3. $seq_ctr = fread($fc,10);
  4. $seq_ctr_next = $seq_ctr;
  5. @$seq_ctr_next = $seq_ctr_next+1;
  6. ftruncate($fc,0);
  7. $ret=fwrite($fc,$seq_ctr_next);
  8. fflush($fc);
  9. flock($fc,LOCK_UN); /* unlock the file */
  10. $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.
Reputation Points: 210
Solved Threads: 228
Nearly a Posting Virtuoso
chrishea is offline Offline
1,389 posts
since Sep 2008
Sep 29th, 2009
0

Re: random generated number integrated into a form

Click to Expand / Collapse  Quote originally posted by ardav ...
@ AB:
Is the $file variable = $countfile? I couldn't see it referenced previously.
Thanks, Ardav, the obvious error, wasnt obvious when I looked at it first time, i searched for bugs, and missed it
php Syntax (Toggle Plain Text)
  1. $form_id = file_get_contents( $countfile ) + 1;
the editor I am trying this time tries to complete the code, great when you can't remember the parameterss, lousy when you walk away to grab a coffee and havent completed the line
Last edited by almostbob; Sep 29th, 2009 at 5:02 pm.
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Sep 30th, 2009
0

Re: random generated number integrated into a form

Hi,
I also faces this same problem then i found solution which i posted here...

PHP Syntax (Toggle Plain Text)
  1. $i=0;
  2. $rand_no="";
  3. while($i<6)
  4. {
  5. $rand_no=$rand_no.rand(0,9);
  6. $i++;
  7. }
  8. echo "6 digit random no.".$rand_no;
  9.  
  10. I hope this code may help you
  11. Thanks
  12. Tulsa
Reputation Points: 13
Solved Threads: 15
Junior Poster in Training
Tulsa is offline Offline
77 posts
since May 2009
Sep 30th, 2009
0

Re: random generated number integrated into a form

Click to Expand / Collapse  Quote originally posted by Tulsa ...
Hi,
I also faces this same problem then i found solution which i posted here...

PHP Syntax (Toggle Plain Text)
  1. $i=0;
  2. $rand_no="";
  3. while($i<6)
  4. {
  5. $rand_no=$rand_no.rand(0,9);
  6. $i++;
  7. }
  8. echo "6 digit random no.".$rand_no;
  9.  
  10. I hope this code may help you
  11. Thanks
  12. Tulsa
Or, a shorter version of what you've got is this:

php Syntax (Toggle Plain Text)
  1. $rand_no = rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
  2. echo $rand_no
Reputation Points: 52
Solved Threads: 27
Junior Poster
edwinhermann is offline Offline
134 posts
since Sep 2009
Sep 30th, 2009
-1

Re: random generated number integrated into a form

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:

PHP Syntax (Toggle Plain Text)
  1. function random_zfill() {
  2. $int = mt_rand(0,999999);
  3. for($i=0; $i<(6-strlen($int)); $i++)$zeroes .= '0';
  4. return $zeroes.$int;
  5. }
  6.  
  7. $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.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 943
Sarcastic Poster
ardav is offline Offline
6,671 posts
since Oct 2006
Sep 30th, 2009
0

Re: random generated number integrated into a form

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TLCJohn is offline Offline
22 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: star ratings
Next Thread in PHP Forum Timeline: # symbol in get statement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC