Web Analytics Made Easy -
StatCounter Creating a text file and then placing in a folder - CodingForum

Announcement

Collapse
No announcement yet.

Creating a text file and then placing in a folder

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a text file and then placing in a folder

    This is how the text file is created.

    PHP Code:
    <?php
            $_SESSION
    ["name"] = $_GET['name'];
            
    $_SESSION["id"] = mt_rand(000000,999999);
            
    $my_file $_SESSION["id"] . '.txt';
            
    $handle fopen($my_file'w') or die('Cannot open file: '.$my_file);
            
    ?>
    It works which is awesome! (I was quite overwhelmed) Now I am wondering if I can place all of these id files into a folder to keep my workspace uncluttered.

    The way I choose to define a users text file is by giving them a id number between 0 and 999,999. They aren't unique though, don't know how to do that.

    I also would like to know if there's a PHP command for searching a text file. My plan would be to do an IF statement on each page and find their steps.

    <body>
    If text file has string cl3, include(r4.txt)
    If text file has string cl2, include(r6.txt)
    </body>

    Something like that

  • #2
    @olefalcon,

    I'm not so familiar with role playing games. How does the flow of the game go? Do you display a paragraph of text (like r4.txt) and then the user types something? What they type determines the next file to show?

    PHP can open up any text file and search for "key words".

    You have a text file for the user, example: 23214.txt
    You have a gazillion text files for each page or paragraph, example r4.txt, r6.txt

    Is there another text file that gets used somehow, for the way the story goes, like the forks and paths?


    Here's something to play with ...

    You have a text file with lines (or rows) of text.
    Let's pick one like r4.txt

    PHP can open that file into an array (of lines). Then you can loop through each line and do things with it.

    <?php
    $data=file("r4.txt");
    foreach($data as $line){
    if(strpos($line,"cl3")){
    echo "cl3 exists on this line: $line";
    }
    }
    ?>

    Comment


    • #3
      The way I choose to define a users text file is by giving them a id number between 0 and 999,999. They aren't unique though, don't know how to do that.

      I also would like to know if there's a PHP command for searching a text file.
      as per that description, you should use a database for that (even a simple, file-based one like SQLite will do) because 1) you can assign text to a user 2) you can search text.
      The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
      André Behrens, NY Times Software Developer

      Comment


      • #4
        It is much easier to do most things with databases as compared to using ordinary files because a database basically is ordinary files with a wrapper for doing all the things you want to be able to do with the data in them.
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment


        • #5
          I'm not so familiar with role playing games. How does the flow of the game go? Do you display a paragraph of text (like r4.txt) and then the user types something? What they type determines the next file to show?
          Is there another text file that gets used somehow, for the way the story goes, like the forks and paths?
          Basically I have it so that you read your little scenario, and you have two or more buttons at the bottom of the page. The buttons will keep taking you to the next page. 001.php, 002.php, 003.php. In the next page whatever button you pressed will give your $_GET variable. I just append that into their text file.

          This is how it writes the new file. (This is on the second page, job.html)
          PHP Code:
           $_SESSION["name"] = $_GET['name'];
                  
          $_SESSION["id"] = mt_rand(000000,999999);
                  
          $my_file $_SESSION["id"] . '.txt';
                  
          $handle fopen($my_file'w') or die('Cannot open file: '.$my_file); 
          After they choose a 'job' this is how it appends it on page class.php.
          PHP Code:
            $_SESSION["job"] = $_GET['job'];
                  
          $my_file =  $_SESSION["id"] . '.txt';
                  
          $handle fopen($my_file'w') or die ('Cannot open file: '.$my_file);
                  
          $data $_SESSION["job"] . '|';
                  
          fwrite($handle$data); 
          I have tested it and it will write to a text file like so:
          456732.txt
          job1|cl4|
          In the 'content' div of my page, I want it to include a certain text file, if a certain string was printed in the text file. When a user pushes a button, it will write which path he took to the text file, and the form will automatically take him to the next page. (001.php, 004.php, 034.php)


          The reason I want these text files that are created to go in a folder is they are cluttering my workspace every time I test my pages. Could I index them under
          'txt/' . $_SESSION . '.txt'
          My folder is txt.

          Comment


          • #6
            I'm also having trouble finding my text files with PHP include. I use c9.io, does anyone recommend a better workspace?
            PHP Code:
            include 'txt/prologue.txt'

            Comment


            • #7
              It does not matter where you put your files. If you create a directory called 'txt', then it would be like your example: include('txt/prologue.txt');

              What if the first line of every .txt file contained the data for the buttons and the path to take? Your script would always use the first line to make the buttons, and the remaining lines will display as the text.

              "prologue.txt" ...

              Go to the church|c6.txt,Go to the valley|v8.txt,Enter the dark forest|f9.txt
              Welcome to my role playing game. blah blah blah

              So you load the file into an array and explode that first line. Each question is separated by commas and then the question itself and it's path is separated by pipes |

              You can use Google to search for tutorials on PHP arrays, and the "explode" function. If you want to set all of this aside and learn MySQLi, then you can do that too. By not using MySQLi, that database topic will become the only discussion you'll see here until you 'comply' with everyone's demands that you use MySQLi. I'm telling you to do whatever you want and ignore anything you don't wish to do ... including anything I mention. It's your website and you are learning PHP. If you have any questions or need help along the way, keep asking.

              Meanwhile, learn about arrays and how to explode that first line of the .txt file.

              Here's a way to search Google:
              https://www.google.com/#q=php+explode

              Put the word PHP first, and then what you are looking for.

              PHP.net is probably the first result you'll get. Open that and read about the function you are looking for, as well as examples.

              Code away!

              Comment


              • #8
                ... and the form will automatically take him to the next page. (001.php, 004.php, 034.php)
                Before you go much further, I recommend that you switch to a single main .php file and use logical pages, where a get parameter in the url tells the code in the one file what to do. This will keep all the common logic and common html in one place, so that you only have to write it and test it once, and make any changes to it in that one file, rather than scattered around in 10's of different code files.

                If you are going to manage your own data files, use file_put_contents(), file_get_contents()/file() to write and read the data. These will result in only a single statement needed to both open the file and write/read the data. Also, by using include, you are open to server-side code injection that will end up getting a public server taken over by hackers.

                Also, if you use data arrays (you are dealing with sets of data, which is what arrays are for) and store the data in the files as either serialized or json encoded data, the data will carry with it its name/index, making it easier and clearer to write, read, and troubleshoot what the code and data are doing. To get the data back, just un-serialize or json decode it.

                And, since you are storing the data in files, there's no good reason to also store it in $_SESSION variables (everything you are doing in the code to manage the file name and read/write data is what php's file based session data handler is already doing). This is just cluttering up your code and causing you unnecessary typing.
                Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

                Comment


                • #9
                  My plan would be to do an IF statement on each page and find their steps.
                  You wouldn't use discrete conditional logic to just map one value to another. You would use an array to map the values -
                  PHP Code:
                  // define an array that maps an input value to an output value
                  $map = array();
                  $map['cl2'] = 'r6';
                  $map['cl3'] = 'r4';

                  $value 'cl3'// value is gotten from wherever it is stored

                  if(!isset($map[$value])) // do some error checking/handling
                  {
                      
                  // value doesn't have an entry
                     
                  $errors[] = 'The requested value in invalid.'// setup an error message
                  }
                  else
                  {
                      
                  // use $map[$value] here to reference the corresponding output value

                  This will produce a 'data driven' design, where you only need to change the data definition to control what the application does. The php program logic stays the same and doesn't get touched no matter how much data there is or what the values get changed to.
                  Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

                  Comment

                  Working...
                  X