Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Random increment of a number contained in a text file

  1. #1
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default Random increment of a number contained in a text file

    Hi guys,

    is there a PHP script that when it runs it could read a number contained in a text file and then able to increase it randomly by either 1, 2 or 3?

    Thanks

    Kenny

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    2,039
    Thanks
    16
    Thanked 316 Times in 314 Posts
    Blog Entries
    13

    Default

    PHP Code:
    $number file_get_contents('number.txt'); // read number from text file
    $random rand(1,3); // generate random number between 1 and 3
    echo $number $random// add numbers together and echo on to page 
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    KennyP (10-11-2013)

  4. #3
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by Beverleyh View Post
    PHP Code:
    $number file_get_contents('number.txt'); // read number from text file
    $random rand(1,3); // generate random number between 1 and 3
    echo $number $random// add numbers together and echo on to page 
    Hi Beverley- thank you very, very much for your reply and the code.

    When I run that code as a php file with my browser, it does add a random number to the number it finds in the text page, and it does display the new number in the browser. However, it does not actually write that new number, $number + $random;, back onto the text page. The text page is not locked to any other files, therefore can the code be made to also write the new number onto the text page?

    Thanks
    Last edited by KennyP; 10-11-2013 at 02:37 AM.

  5. #4
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by KennyP View Post
    Hi Beverley- thank you very, very much for your reply and the code.

    When I run that code as a php file with my browser, it does add a random number to the number it finds in the text page, and it does display the new number in the browser. However, it does not actually write that new number, $number + $random;, back onto the text page. The text page is not locked to any other files, therefore can the code be made to also write the new number onto the text page?

    Thanks
    I tried using
    PHP Code:
    print $number $random
    and also tried giving writing permissions to PHP with
    PHP Code:
    $handle fopen($number'w+') ; 
    but I don't know enpough to make it work. Any further help would be very much appreciated.

    Thanks

  6. #5
    Join Date
    Mar 2011
    Location
    N 11° 19' 0.0012 E 142° 15' 0
    Posts
    1,531
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Try
    PHP Code:
    $number file_get_contents('number.txt'); // read number from text file
    $random rand(1,3); // generate random number between 1 and 3
    $output $number $random// add numbers together and echo on to page  
    file_put_contents ('number.txt' $output); //Rewrite file with new data 

  7. The Following User Says Thank You to keyboard For This Useful Post:

    KennyP (10-11-2013)

  8. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    What's your full code? You'll need more than just fopen.

    Usually you need a combination of:
    fopen()
    fwrite()
    fclose()

    Newer versions of PHP may also allow:
    file_put_contents()


    For a full example, see php.net's documentation for fwrite().


    --
    Edit: I hadn't noticed keyboard's relatively new post. That's a simple answer, if your version of PHP is new enough (and that function is enabled-- I think sometimes that one might be disabled for security or something).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. The Following User Says Thank You to djr33 For This Useful Post:

    KennyP (10-11-2013)

  10. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    [file_put_contents is] a simple answer, if your version of PHP is new enough (and that function is enabled-- I think sometimes that one might be disabled for security or something).
    There haven't been any changes to it since 5.1, so (I certainly hope!) there are no issues with your version of PHP being new enough.

    Hosts usually don't disable it (unless they disable all file writing-related functions), but it may have a directory restriction imposed on it, and it is very likely that stream contexts (e.g., writing a file to a URL, or pipes, etc.) will be disabled.

    But none of that should be an issue in this case—if you're allowed to write at all, file_put_contents will almost certainly be workable, and is probably the best (most straightforward) solution.

  11. The Following User Says Thank You to traq For This Useful Post:

    KennyP (10-11-2013)

  12. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't know exactly what was wrong, but a while ago I couldn't get file_put_contents() to work (it was never clear why), but fopen+fwrite+fclose did the same thing just fine. So I've actually never switched over. Although I only use file_get_contents() when I'm just reading, and that always works.
    And all of that was, as far as I know, only PHP 5. I suppose it's possible I was at some point working on a PHP 4 server when it didn't work, but at the time I tried it a few times and never got it to work, so I gave up.

    KennyP, if file_put_contents() works for you, that's great, and you should just ignore what I'm saying here
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  13. The Following User Says Thank You to djr33 For This Useful Post:

    KennyP (10-11-2013)

  14. #9
    Join Date
    Dec 2009
    Location
    NY NY USA
    Posts
    142
    Thanks
    102
    Thanked 1 Time in 1 Post

    Default

    Thanks very much for your replies guys.

    The PHP version is 5.5.32, and there aren't any writing restrictions. Still, the code above is not writing to the text file (set to 777), and now it doesn't even display in the browser as Beverlyh's code. Any suggestions?

  15. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Have you tried the slightly more complicated fwrite() approach? It always works for me.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  16. The Following User Says Thank You to djr33 For This Useful Post:

    KennyP (10-11-2013)

Similar Threads

  1. Add increment number to class?
    By gwmbox in forum JavaScript
    Replies: 2
    Last Post: 06-03-2010, 01:31 AM
  2. Random Number
    By Rockonmetal in forum PHP
    Replies: 5
    Last Post: 09-16-2007, 10:30 PM
  3. Random number addition script - help!
    By Shenshen in forum JavaScript
    Replies: 1
    Last Post: 05-23-2006, 07:17 PM
  4. Help - Random Number script..
    By kamisaibou in forum JavaScript
    Replies: 2
    Last Post: 03-02-2006, 10:46 PM
  5. Random Iframe Laoding TEXT FILE
    By marcchiesa in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 03-09-2005, 09:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •