Read and Write file contents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #31
    Hi,

    That's about it, I only mentioned print_r() to aid your understanding.

    Without that line in, and adjusting the output you get:

    [php]
    <?php
    $file = fopen("/var/lib/penguin/Desktop/welcome.txt","r ")or exit("Unable to open file!");
    $x = 0;
    for($i = 0 ; $i != feof($file) ; $i++)
    {
    $data = fgets($file,$x) ;
    $str = explode(':', $data);

    for($j = 0; $j < count($str); $j++)
    {
    echo $str[$j] . "<br />";
    }
    $x=$x+1024;
    }
    $lcount=$i;
    ?
    [/php]

    This should, I beleive, output the contents of the file to screen.

    Now, run it and see what you get. This should enable you to see what you still need to do.

    Cheers
    nathj

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #32
      Originally posted by nathj
      Hi,

      That's about it, I only mentioned print_r() to aid your understanding.

      Without that line in, and adjusting the output you get:

      [php]
      <?php
      $file = fopen("/var/lib/penguin/Desktop/welcome.txt","r ")or exit("Unable to open file!");
      $x = 0;
      for($i = 0 ; $i != feof($file) ; $i++)
      {
      $data = fgets($file,$x) ;
      $str = explode(':', $data);

      for($j = 0; $j < count($str); $j++)
      {
      echo $str[$j] . "<br />";
      }
      $x=$x+1024;
      }
      $lcount=$i;
      ?
      [/php]

      This should, I beleive, output the contents of the file to screen.

      Now, run it and see what you get. This should enable you to see what you still need to do.

      Cheers
      nathj
      this is wht is getting printed in the o/p

      "; } $x=$x+1024; } $lcount=$i; ?>
      "); $i = $i + 1; } ?>


      this is the code tht i am using


      [PHP] <?php
      $file = fopen("/var/lib/penguin/Desktop/welcome.txt","r ")or exit("Unable to open file!");
      $x = 0;
      for($i = 0 ; $i != feof($file) ; $i++)
      {
      $data = fgets($file,$x) ;
      $str = explode(':', $data);

      for($j = 0; $j < count($str); $j++)
      {
      echo $str[$j] . "<br />";//lets say $str[0] contains "name" and $str[1] contains "pradeep" .but as we come to 2nd line of the file and this for loop the str[0] wll contain "place" and and str[1] wll contain "bangalore" .this wll be a prob in html calling ..

      }
      $x=$x+1024;
      }
      $lcount=$i;
      ?>
      [/PHP]
      [HTML]<html>
      <head>
      </head>
      <body>

      <FORM METHOD=post ACTION="test1.p hp">

      <?
      print $lcount;
      for($i=0;$i <= $lcount ;$i++){

      print("<input name=" . $str[$i] ." type=text value=" . $str[$i+1] ." >");


      $i = $i + 1;
      }

      ?>
      <INPUT TYPE=SUBMIT>
      </form>
      </body>
      </html>
      [/HTML]

      ok how to call the in the html separetely i.e name and pradeep..
      ie the it shld be lke
      the html page must be lke this

      name:<input type="text" value="pradeep>
      place:<input type="text" value="bangalor e>

      see line 11

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #33
        Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

        name:pradeep&&l ocation:bangalo re||name:smith& &location:somew here

        Without the need for the low level file functions, as you have no line breaks you do:
        [php]
        <?php

        $lcFileContents = file(_FileName_ );
        $laContents = explode("||", $lcFileContents ); // element 0 = name:pradeep&&l ocation:bangalo re

        $lnArrayLength = count($laConten ts);
        for(i=0; i<=$lnArrayLeng th; $i++)
        {
        $lcMainSet = $laContents[$i];
        $laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

        // nest another appropriate loop and youre away
        }
        ?>
        [/php]

        Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

        Have a play and let me know how you get on.

        Cheers
        nathj

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #34
          Originally posted by nathj
          Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

          name:pradeep&&l ocation:bangalo re||name:smith& &location:somew here

          Without the need for the low level file functions, as you have no line breaks you do:
          [php]
          <?php

          $lcFileContents = file(_FileName_ );
          $laContents = explode("||", $lcFileContents ); // element 0 = name:pradeep&&l ocation:bangalo re

          $lnArrayLength = count($laConten ts);
          for(i=0; i<=$lnArrayLeng th; $i++)
          {
          $lcMainSet = $laContents[$i];
          $laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

          // nest another appropriate loop and youre away
          }
          ?>
          [/php]

          Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

          Have a play and let me know how you get on.

          Cheers
          nathj


          the idea got rejected from my boss...the file structure is same.
          name:pradeep
          place:bangalore
          addess:bsk

          Comment

          • pradeepjain
            Contributor
            • Jul 2007
            • 563

            #35
            Originally posted by nathj
            Just a suggestion but you could format the file differently. So that you have || to separate between full items such as the name and place combination and then && to separate between sub entities. This would give you file like

            name:pradeep&&l ocation:bangalo re||name:smith& &location:somew here

            Without the need for the low level file functions, as you have no line breaks you do:
            [php]
            <?php

            $lcFileContents = file(_FileName_ );
            $laContents = explode("||", $lcFileContents ); // element 0 = name:pradeep&&l ocation:bangalo re

            $lnArrayLength = count($laConten ts);
            for(i=0; i<=$lnArrayLeng th; $i++)
            {
            $lcMainSet = $laContents[$i];
            $laSubArray = explode["&&", $lcMainSet]; //elemnt 0 = name:pradeep

            // nest another appropriate loop and youre away
            }
            ?>
            [/php]

            Hopefully get the idea, the use of nested loops can get messy so be warned and write useful comments in the code to help sign post the process to anyone else who reads it.

            Have a play and let me know how you get on.

            Cheers
            nathj

            ANY OTHER SUGGESTION AS HOW TO CONTINUE WITH THE OLD FILE STRUCTURE ITSELF....

            Comment

            • nathj
              Recognized Expert Contributor
              • May 2007
              • 937

              #36
              Originally posted by pradeepjain
              ANY OTHER SUGGESTION AS HOW TO CONTINUE WITH THE OLD FILE STRUCTURE ITSELF....
              First, please don't shout at me. You are not paying me, I am helping you because I am kind that way.

              Second, you need to take the code you've got and try different things. It may be that you have to hard code some rules into the php. I know it sounds nasty but it's not that bad.

              Please stop panicing and this project, take a break and then play around with what you've got. My mantra is keep trying and keep learning. That's exactly what you need to do.

              I've provided you psuedo code and code samples. It's about time you did something for yourself. As you can see my patience is running out.

              nathj

              Comment

              • pradeepjain
                Contributor
                • Jul 2007
                • 563

                #37
                Originally posted by nathj
                First, please don't shout at me. You are not paying me, I am helping you because I am kind that way.

                Second, you need to take the code you've got and try different things. It may be that you have to hard code some rules into the php. I know it sounds nasty but it's not that bad.

                Please stop panicing and this project, take a break and then play around with what you've got. My mantra is keep trying and keep learning. That's exactly what you need to do.

                I've provided you psuedo code and code samples. It's about time you did something for yourself. As you can see my patience is running out.

                nathj


                hey really srry if u felt lke i was shouting on u...
                hey look at this code .i hve done it properly but its still not working can u try this and tell me


                [HTML] <html>
                <head>
                </head>
                <body>


                <?php

                $filehandle = fopen("/var/www/html/welcome.txt","r ");

                $line=true;

                echo '<form method=post action=action.p hp>';

                while($filehand le && $line){

                $line = fgets($filehand le);

                $str = explode(':', $line);

                echo '<input name="'.$str[0].'" value="'.$str[1].'"><br />';
                }

                echo '<input type=submit value= submit>';
                echo '</form>'
                ?>
                </body>
                </html>
                [/HTML]

                Comment

                • nathj
                  Recognized Expert Contributor
                  • May 2007
                  • 937

                  #38
                  Originally posted by pradeepjain
                  hey really srry if u felt lke i was shouting on u...
                  hey look at this code .i hve done it properly but its still not working can u try this and tell me


                  [HTML] <html>
                  <head>
                  </head>
                  <body>


                  <?php

                  $filehandle = fopen("/var/www/html/welcome.txt","r ");

                  $line=true;

                  echo '<form method=post action=action.p hp>';

                  while($filehand le && $line){

                  $line = fgets($filehand le);

                  $str = explode(':', $line);

                  echo '<input name="'.$str[0].'" value="'.$str[1].'"><br />';
                  }

                  echo '<input type=submit value= submit>';
                  echo '</form>'
                  ?>
                  </body>
                  </html>
                  [/HTML]

                  Apology accepted.

                  The final echo line doesn't have a semi-colon on the end.

                  When yo usay it's nt working what exactly are you getting on screen and in the generated source code?

                  Also turn on the error reporting in php and see what it tells you, this may give you a clue as to what is going on.

                  All in all it looks like a nice solution.

                  Cheers
                  nathj

                  Comment

                  • pradeepjain
                    Contributor
                    • Jul 2007
                    • 563

                    #39
                    Originally posted by nathj
                    Apology accepted.

                    The final echo line doesn't have a semi-colon on the end.

                    When yo usay it's nt working what exactly are you getting on screen and in the generated source code?

                    Also turn on the error reporting in php and see what it tells you, this may give you a clue as to what is going on.

                    All in all it looks like a nice solution.

                    Cheers
                    nathj
                    its printing the echo commands at it is lke its giving text box and inside it its giving $str[1]..i am not able to properly explain whts ..so i asked u to see it once by trying it.

                    hey i added this file to /var/www/html/ and then within html i created welcome.txt
                    and the accessed it in webbrowser by http://localhost/i.html(html filename)..is this process correct..

                    Comment

                    • nathj
                      Recognized Expert Contributor
                      • May 2007
                      • 937

                      #40
                      Hi pradeepjain,

                      I have tried this with the following file:

                      Code:
                      name:micahel
                      location:Newcastle
                      name:Steven
                      location:Liverpool
                      name:Jamie
                      location:Liverpool
                      Using the following php
                      [php]
                      <?php
                      $filehandle = fopen("test.txt ","r");
                      $line=true;
                      echo '<form method=post action=action.p hp>';
                      while($filehand le && $line)
                      {
                      $line = fgets($filehand le);
                      if (!empty($line))
                      {
                      $str = explode(':', $line);
                      echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';
                      }
                      }

                      echo '<input type=submit value= submit>';
                      echo '</form>'
                      ?>
                      [/php]

                      This gives me a page with 6 text boxes and labels on.

                      The text file is currently in the same directory as the php file but that shouldn't make any difference.

                      Hopefully you will get this finished in time for Saturday and will have learnt a bit more about PHP.

                      Please take some time after this project to read some php tutorials. This will give you a better understanding of the basics and help you to get better at it very quickly.

                      Cheers
                      nathj

                      Comment

                      • pradeepjain
                        Contributor
                        • Jul 2007
                        • 563

                        #41
                        Originally posted by nathj
                        Hi pradeepjain,

                        I have tried this with the following file:

                        Code:
                        name:micahel
                        location:Newcastle
                        name:Steven
                        location:Liverpool
                        name:Jamie
                        location:Liverpool
                        Using the following php
                        [php]
                        <?php
                        $filehandle = fopen("test.txt ","r");
                        $line=true;
                        echo '<form method=post action=action.p hp>';
                        while($filehand le && $line)
                        {
                        $line = fgets($filehand le);
                        if (!empty($line))
                        {
                        $str = explode(':', $line);
                        echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';
                        }
                        }

                        echo '<input type=submit value= submit>';
                        echo '</form>'
                        ?>
                        [/php]

                        This gives me a page with 6 text boxes and labels on.

                        The text file is currently in the same directory as the php file but that shouldn't make any difference.

                        Hopefully you will get this finished in time for Saturday and will have learnt a bit more about PHP.

                        Please take some time after this project to read some php tutorials. This will give you a better understanding of the basics and help you to get better at it very quickly.

                        Cheers
                        nathj
                        wht u ment to say is tht this code is working properly or not..ok 6 text boxes are coming but is the contents being displayed properly..
                        lke
                        name(label) micheal(with in a text box) in the page

                        Comment

                        • nathj
                          Recognized Expert Contributor
                          • May 2007
                          • 937

                          #42
                          Yes! It is working correctly. The item on the left of the : in the txt file is the label and the item on the right of the : in the ext file is the contents of the input box.

                          If you could have been bothered to run this yourself you would see that it is working in the way you described.

                          Please run this and see! This is after all you rproject and I feel that you are doing very little to help yourself. I keep helping you becasue I am kind and like to see everyone succeed but if you do not help yourself a bit more I'll disappear.

                          Cheers
                          nathj

                          Comment

                          • pradeepjain
                            Contributor
                            • Jul 2007
                            • 563

                            #43
                            Originally posted by nathj
                            Yes! It is working correctly. The item on the left of the : in the txt file is the label and the item on the right of the : in the ext file is the contents of the input box.

                            If you could have been bothered to run this yourself you would see that it is working in the way you described.

                            Please run this and see! This is after all you rproject and I feel that you are doing very little to help yourself. I keep helping you becasue I am kind and like to see everyone succeed but if you do not help yourself a bit more I'll disappear.

                            Cheers
                            nathj


                            ya ok i agree on wht u r saying..but i hve tried it ...but its not working for me here...see this is the code tht i am using.....

                            [HTML]<html>
                            <head>
                            </head>
                            <body>
                            <?php

                            $filehandle = fopen("welcome. txt","r");

                            $line=true;

                            echo '<form method=post action=action.p hp>';

                            while($filehand le && $line)

                            {

                            $line = fgets($filehand le);

                            if (!empty($line))

                            {

                            $str = explode(':', $line);

                            echo $str[0] . ': <input type="text" name="'.$str[0].'" value="'.$str[1].'"><br />';

                            }

                            }



                            echo '<input type=submit value= submit>';

                            echo '</form>';

                            ?>

                            </body>
                            </html>
                            [/HTML]

                            u might be saying tht i am not doing any thing..but i am putting a lot of effort..i am reading tht php bible...

                            Comment

                            • nathj
                              Recognized Expert Contributor
                              • May 2007
                              • 937

                              #44
                              In what whay is this not working for you?

                              Do you have permission to read the text file on your system?

                              I do not know what you mean when you say it is not working for you. The code I gave in my previous post works just fine for me.

                              The code you supplied alos works for me. When I change the file name to the test file I have set up it works perfectly. I do not understand what the problem is now.

                              Cheers
                              nathj

                              Comment

                              • pradeepjain
                                Contributor
                                • Jul 2007
                                • 563

                                #45
                                Originally posted by nathj
                                In what whay is this not working for you?

                                Do you have permission to read the text file on your system?

                                I do not know what you mean when you say it is not working for you. The code I gave in my previous post works just fine for me.

                                The code you supplied alos works for me. When I change the file name to the test file I have set up it works perfectly. I do not understand what the problem is now.

                                Cheers
                                nathj
                                i am jst executing the code i passed u in browser by giving http://localhost/i.html is there any other php settings tht i hve to do..

                                Comment

                                Working...