Read and Write file contents

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

    #16
    Originally posted by pradeepjain
    i hve written this code...there is some error in the html combining wit the php..
    can any one sujjest me something....
    Good to see you have written some code.

    Try turning on the error hanlding and telling what you see on the page when you run it. This will shed some light on what is happening.

    Also when yo say 'there is some error' what do you mean? What are you currently seeing, what do you think is happening.

    My old university tutor would have sent me packing if I'd said 'there's some error please help.' You have to define the problem a bit more, especially as we cannot see your system/setup completely but just the code you post (thanks for posting it).

    Let us know what comes back from your next set of tests.

    Cheers
    nathj

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #17
      Originally posted by nathj
      Good to see you have written some code.

      Try turning on the error hanlding and telling what you see on the page when you run it. This will shed some light on what is happening.

      Also when yo say 'there is some error' what do you mean? What are you currently seeing, what do you think is happening.

      My old university tutor would have sent me packing if I'd said 'there's some error please help.' You have to define the problem a bit more, especially as we cannot see your system/setup completely but just the code you post (thanks for posting it).

      Let us know what comes back from your next set of tests.

      Cheers
      nathj

      oh ok....its not showning anything in the form jst form text box is appearing and then within it it appears
      for($i=0;$i <= $lcount ;$i++){ type=text > } lke this in html page

      Comment

      • gregerly
        Recognized Expert New Member
        • Sep 2006
        • 192

        #18
        First thing that jumps out at me is that you have PHP code not inside PHP tags:

        [HTML]<FORM METHOD=post ACTION="test1.p hp">
        for($i=0;$i <= $lcount ;$i++){
        <input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

        }
        <INPUT TYPE=SUBMIT>
        </form>[/HTML]

        which should be:

        [HTML]<FORM METHOD=post ACTION="test1.p hp">[/HTML]
        [PHP]<?
        for($i=0;$i <= $lcount ;$i++){
        <input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

        }
        ?>[/PHP]
        [HTML]<INPUT TYPE=SUBMIT>
        </form>[/HTML]

        Greg

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #19
          Originally posted by gregerly
          First thing that jumps out at me is that you have PHP code not inside PHP tags:

          [HTML]<FORM METHOD=post ACTION="test1.p hp">
          for($i=0;$i <= $lcount ;$i++){
          <input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

          }
          <INPUT TYPE=SUBMIT>
          </form>[/HTML]

          which should be:

          [HTML]<FORM METHOD=post ACTION="test1.p hp">[/HTML]
          [PHP]<?
          for($i=0;$i <= $lcount ;$i++){
          <input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

          }
          ?>[/PHP]
          [HTML]<INPUT TYPE=SUBMIT>
          </form>[/HTML]

          Greg

          i tried wht u told but not working
          error:type=text > } ?>

          Comment

          • nathj
            Recognized Expert Contributor
            • May 2007
            • 937

            #20
            Hi,

            Try changing this section

            [PHP]
            <?
            for($i=0;$i <= $lcount ;$i++){
            <input name= <?php $name[$i] ?> type=text > <?php $value[$i] ?>

            }
            ?>

            [/PHP]

            To something more like this:
            [PHP]
            <?php
            for ($i=0; $i <= $lcount; $i++)
            {
            echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;
            }

            ?>
            [/PHP]
            The first sample has nested php tags, I don't know if this is a problem or not but it sure looks weird. Also the raw HTML inside the php tags is a potential problem.
            The second sample removes the need for this and should work just fine, assuming that the two arrays are correct.

            So, your final page could look like this (all in php):
            [PHP]
            <?php
            echo '<form name = "testform" method="post" action="test1.p hp">
            ';
            for ($i=0; $i <= $lcount; $i++)
            {
            echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;
            }
            echo '<input type="submit" value="Complete "/>';
            echo '</form>';
            ?>
            [/PHP]


            Have a play with this and let us know what you get.

            Cheers
            nathj

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #21
              <?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);
              $name[$i] = $str[0]; // piece1
              $value[$i] = $str[1]; // piece2
              echo $name[$i];
              echo $value[$i];
              $x=$x+1024;
              $i=$i+1;
              }
              $lcount=$i;
              ?>
              <html>
              <head>
              </head>
              <body>


              <?php

              echo '<form name = "testform" method="post" action="test1.p hp">

              ';

              for ($i=0; $i <= $lcount; $i++)

              {

              echo '<input type="text" name=' . $name[$i] . 'value=' . $value[$i] . ' />' ;

              $i = $i + 1;
              }

              echo '<input type="submit" value="Complete "/>';

              echo '</form>';

              ?>



              </form>
              </body>
              </html>

              this is wht i hve used as code..but still not working...

              error:'; for ($i=0; $i <= $lcount; $i++) { echo '' ; $i = $i + 1; } echo ''; echo ''; ?>


              i thnk there is a problem in 2 arrays...is th 1st php code proper

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #22
                What information is in the two arrays you have? It may be that we need to adjust the code to include quote marks if the data in the array contains spaces.


                Also are you getting an error message - what is the message exactly?

                Cheers
                nathh

                Comment

                • pradeepjain
                  Contributor
                  • Jul 2007
                  • 563

                  #23
                  Originally posted by nathj
                  What information is in the two arrays you have? It may be that we need to adjust the code to include quote marks if the data in the array contains spaces.


                  Also are you getting an error message - what is the message exactly?

                  Cheers
                  nathh

                  the file contains
                  name:pradeep
                  palce:blore

                  i am taking the 1st line into data and then using explode so as 2 split into 2 arrays say name and pradeep..and storing them in name[$i] and value[$i]
                  then i am moving to next line wit help of $x+1024 and then splitting the 2nd line also..
                  then using the variables in the html form..
                  there is no proper error ..but its displaying jst tht wht i said in last msg.

                  Comment

                  • nathj
                    Recognized Expert Contributor
                    • May 2007
                    • 937

                    #24
                    Originally posted by pradeepjain
                    the file contains
                    name:pradeep
                    palce:blore

                    i am taking the 1st line into data and then using explode so as 2 split into 2 arrays say name and pradeep..
                    explode() will generate one array:
                    [PHP]
                    <?php
                    $lcFileContent = file(_FileName_ );
                    $laTest = explode(":", $lcFileContent) ;
                    print_r($laTest );
                    [/PHP]

                    This single array will then contain 'name' and 'pradeep'. The code abpve will print the array and you will be able to see what is in it and how best to manipulate.

                    Cheers
                    nathj

                    Comment

                    • pradeepjain
                      Contributor
                      • Jul 2007
                      • 563

                      #25
                      Originally posted by nathj
                      explode() will generate one array:
                      [PHP]
                      <?php
                      $lcFileContent = file(_FileName_ );
                      $laTest = explode(":", $lcFileContent) ;
                      print_r($laTest );
                      [/PHP]

                      This single array will then contain 'name' and 'pradeep'. The code abpve will print the array and you will be able to see what is in it and how best to manipulate.

                      Cheers
                      nathj

                      but this is wht in found i php manual

                      If you want to split a price (float) into pounds and pence.

                      or dollors and cents etc etc.

                      $price = "6.20";

                      $split = explode(".", $price);
                      $pound = $split[0]; // piece1
                      $pence = $split[1]; // piece2

                      echo "&pound $pound . $pence\n";

                      seeing this only i used tht...

                      hey i am not finding a way wht to do..if i dont show the code before SATURDAY i wll kicked off frm the job..,

                      Comment

                      • nathj
                        Recognized Expert Contributor
                        • May 2007
                        • 937

                        #26
                        Originally posted by pradeepjain
                        but this is wht in found i php manual

                        If you want to split a price (float) into pounds and pence.

                        or dollors and cents etc etc.

                        $price = "6.20";

                        $split = explode(".", $price);
                        $pound = $split[0]; // piece1
                        $pence = $split[1]; // piece2

                        echo "&pound $pound . $pence\n";

                        seeing this only i used tht...

                        hey i am not finding a way wht to do..if i dont show the code before SATURDAY i wll kicked off frm the job..,
                        First of all please use CODE tags, it makes reading your post much easier!

                        Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

                        The code sample you posted
                        [PHP]
                        $price = "6.20";

                        $split = explode(".", $price);
                        $pound = $split[0]; // piece1 - "6"
                        $pence = $split[1]; // piece2 - "20"

                        echo "&pound $pound . $pence\n";
                        [/PHP]
                        Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

                        In your case all that is different is the source of the original string - you want it to come from a file.

                        What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

                        Open the file and loop through one line at a time.
                        For each line create a variable of the line.
                        Explode the variableit into an array
                        Loop through this array (this is now a nested loop)
                        This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

                        This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

                        If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

                        nathj

                        Comment

                        • ak1dnar
                          Recognized Expert Top Contributor
                          • Jan 2007
                          • 1584

                          #27
                          pradeepjain,

                          Please read the Forum rules and Posting guidelines while you using the forums.And please pay your kind attention to these;

                          Comment

                          • pradeepjain
                            Contributor
                            • Jul 2007
                            • 563

                            #28
                            Originally posted by nathj
                            First of all please use CODE tags, it makes reading your post much easier!

                            Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

                            The code sample you posted
                            [PHP]
                            $price = "6.20";

                            $split = explode(".", $price);
                            $pound = $split[0]; // piece1 - "6"
                            $pence = $split[1]; // piece2 - "20"

                            echo "&pound $pound . $pence\n";
                            [/PHP]
                            Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

                            In your case all that is different is the source of the original string - you want it to come from a file.

                            What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

                            Open the file and loop through one line at a time.
                            For each line create a variable of the line.
                            Explode the variableit into an array
                            Loop through this array (this is now a nested loop)
                            This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

                            This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

                            If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

                            nathj


                            [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);

                            print_r($str); //ok as u said this wll contain name pradeep

                            //how to use 2nd loop..i did not get u on this.


                            $x=$x+1024;
                            $i=$i+1;
                            }
                            $lcount=$i;
                            ?>[/PHP]

                            Comment

                            • pradeepjain
                              Contributor
                              • Jul 2007
                              • 563

                              #29
                              If u cld show how to use 2nd loop and how to store "name" and "pradeep" in 2 diff variables...i wll b graetful to u..

                              Comment

                              • pradeepjain
                                Contributor
                                • Jul 2007
                                • 563

                                #30
                                Originally posted by nathj
                                First of all please use CODE tags, it makes reading your post much easier!

                                Second, you may be in a panic but I'm just trying to help and perhaps you need to take a breather before reading this and come back to it with a clearer mind. I know I need to do that sometimes.

                                The code sample you posted
                                [PHP]
                                $price = "6.20";

                                $split = explode(".", $price);
                                $pound = $split[0]; // piece1 - "6"
                                $pence = $split[1]; // piece2 - "20"

                                echo "&pound $pound . $pence\n";
                                [/PHP]
                                Simply takes string and splits on the occurrence of '.' into one array called $split. This array has numeric keys, starting at 0 and can be referenced accoridingly.

                                In your case all that is different is the source of the original string - you want it to come from a file.

                                What you need to do, is what you were doing earlier (I showed the file() function and print_r() to help you understand what happens with the xplode function).

                                Open the file and loop through one line at a time.
                                For each line create a variable of the line.
                                Explode the variableit into an array
                                Loop through this array (this is now a nested loop)
                                This array will, according to your data structure, contain a pair of values, do what you want with these. Note; as its a numeric array it is simple to loop with a for loop based on the count() of the array - remember arrasy start counting at 0.

                                This is the psuedo code for you. Just write it up. I've given enough help now on this. I do have a job to do myself you know, with my own deadlines to meet - you're not the only one under pressure.

                                If you write the code, as outlined, and need some help post it back here with lots of information and use the CODE or PHP tags to format it.

                                nathj

                                I HVE TRIED THIS IS THIS WHT U SAID TO DO

                                [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);
                                print_r($str);
                                for($j = 0; $j < count($str); $j++){
                                echo "str #$j = $str[$j]<br />";
                                }
                                $x=$x+1024;
                                }
                                $lcount=$i;
                                ?>
                                [/PHP]

                                Comment

                                Working...