Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

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

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

    Default

    Thanks for your reply djr. I'd love to try the fwrite() approach, if you would please provide it; I don't have the expertise to write it.

  2. #12
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    As I said, you can get the information on the php.net documentation page (which you should use in general-- it's incredibly useful; I use it all the time).
    http://www.php.net/manual/en/function.fwrite.php

    There's a lot of information there, so use what you'd like (the one downside to the website is that it can at times appear overly technical, but there's also basic information if you look closely).

    This example is the simplest:
    PHP Code:
    <?php
    $fp 
    fopen('data.txt''w');
    fwrite($fp'1');
    fwrite($fp'23');
    fclose($fp);

    // the content of 'data.txt' is now 123 and not 23!
    ?>
    Of course you don't want that extra bit, so just this is fine:
    PHP Code:
    <?php
    $fp 
    fopen('data.txt''w');
    fwrite($fp'1');
    fclose($fp);
    ?>
    Just change that to the file/content you want, and that'll be fine.

    Of course I don't know if there still might be some permission issues (that can at times be tricky) or even whether your server disables writing to files entirely. See if that can work.

    If it still doesn't work, then try this as a new .php file:
    PHP Code:
    <?php phpinfo(); ?>
    That'll give you information about the configuration and you should be able to see whether it says anything about something like "safe mode" or other reasons that fwrite() might not work.
    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

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

    KennyP (10-11-2013)

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

    Default

    Thanks, but how are the following functions translated into that code?
    Code:
    $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

  5. #14
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    You should try to play with it to see how the code works. $random is a variable, and you can put that in place of '1' in fwrite:

    PHP Code:
    $fp fopen('FILENAME.txt''w');
    fwrite($fp$random);
    fclose($fp); 
    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

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

    KennyP (10-11-2013)

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

    Default

    Thanks djr. Nothing I tried works, because I really don't know what I'm doing.
    Last edited by KennyP; 10-11-2013 at 08:44 AM.

  8. #16
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,994
    Thanks
    43
    Thanked 3,198 Times in 3,160 Posts
    Blog Entries
    12

    Default

    This all seems pretty silly to me - of course I'm only somewhat familiar with PHP. But if file writing privileges are on and the version is what you say it is, you really shouldn't need to bother with anything other than file_put_contents for writing a file. And, if you're worried about the file permissions on the individual file, have PHP create it in the first place. Delete or rename number.txt and use this code:

    PHP Code:
    <?php
    @$number file_get_contents('number.txt') or 0//open and read number from the file if it exists, or start from 0
    $random rand(1,3); // generate random number between 1 and 3
    $output $number $random// add numbers together  
    file_put_contents ('number.txt' $output); //write or overwrite file with result
    echo $output//echo the result
    ?>
    The browser cache may need to be cleared and/or the page refreshed to see changes.

    If you really must try it with fopen, etc., here it is using that instead:

    PHP Code:
    <?php
    @$number file_get_contents('number.txt') or 0//open and read number from the file if it exists, or start from 0 
    $random rand(1,3); // generate random number between 1 and 3 
    $output $number $random// add numbers together 
    $fp fopen('number.txt''w'); // w/next 2 lines, write or overwrite file with result
    fwrite($fp$output);
    fclose($fp); 
    echo 
    $output//echo the result
    ?>
    Again, to be on the safe side remove or rename number.txt to get it out of the way. The code will create its own.

    If neither of these work, it's pretty safe to say that the host does not have file writing privileges turned on. That's different than the setting for the individual file. It's a setting that governs whether or not PHP is allowed to write to any file. It might be something else, but that's the most likely explanation.
    Last edited by jscheuer1; 10-11-2013 at 02:21 PM. Reason: add second version
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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

    KennyP (10-11-2013)

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

    Default

    Thank you very much John. I actually stopped in to post the following code that finally worked for me after lots of trial and error...
    Code:
    <?php
    $number = file_get_contents('test.txt'); // read number from text file
    $random = rand(1,3); // generate random number between 1 and 3
    file_put_contents('test.txt',($number + $random)); // add numbers together and write the total back onto the text file
    ?>
    It's similar to your code. I'm eager to try yours when I get back later today.

    Thanks again

  11. #18
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    KennyP, I'm glad it ended up working.

    But if file writing privileges are on and the version is what you say it is, you really shouldn't need to bother with anything other than file_put_contents for writing a file.
    John, that seems like it should be the case (and in fact turned out to be the case here). But for some reason I really did have problems with file_put_contents() a while ago (and still don't use it now, though perhaps after this I'll start trying it again). In the end, it's irrelevant here, but I wasn't sure above if that might be the problem or not. Trial and error
    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

Similar Threads

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