while loops?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick

    while loops?

    I'm trying to while loop using a flat txt file and write the values out
    into a form. I have 50-58 values to echo. Am I going to have to echo
    each line of html. Or is there a way to make this work between the php.
    Right now it wants to write the whole table over and over. Is my logic
    screwed up? I am not a programmer as such but can get by. I have
    something like this which is an abbreviated version. Any help would be
    appreciated.

    Thanks,
    Patrick

    <html>
    <head>
    <?
    $filename = "C10AB_precruis e.log";
    $larray = file($filename) ;
    ?>
    </head>
    <body>
    <?
    $i=0;
    while ($i <= 60)
    {
    ?>

    <form method="post" action="precmlo g.php">

    <table width="800" cellpadding="5" cellspacing="0" border="0">
    <tr>
    <td width="40%">
    Log sheet completed by:<br> <input class="inform" type="text"
    size="30" name="person" value="<?php echo $larray[$i]; ?>"><br>
    </td>
    </tr>
    <tr>
    <td>
    Cruise name/number:<br> <input class="inform" type="text" size="30"
    name="cruisenum " value="<?php echo $larray[$i]; ?>"><br>
    </td>
    </tr>

    <tr>
    <td>
    Ship name:<br> <input class="inform" type="text" size="30"
    name="shipname" value="<?php echo $larray[$i]; ?>"><br>
    </td>
    </tr>
    </table>

    <input class="button" type="submit" value="Submit">
    </form>


    <?
    }
    $i++;
    ?>
    </body>
    </html>

    --
    Patrick A. Smith Assistant System Administrator
    Ocean Circulation Group – USF - College of Marine Science
    http://ocgweb.marine.usf.edu Phone: 727 553-3334

    The trouble with doing something right the first time is that nobody
    appreciates how difficult it was. - La Rochefoucauld


  • David Haynes

    #2
    Re: while loops?

    Patrick wrote:[color=blue]
    > I'm trying to while loop using a flat txt file and write the values out
    > into a form. I have 50-58 values to echo. Am I going to have to echo
    > each line of html. Or is there a way to make this work between the php.
    > Right now it wants to write the whole table over and over. Is my logic
    > screwed up? I am not a programmer as such but can get by. I have
    > something like this which is an abbreviated version. Any help would be
    > appreciated.
    >
    > Thanks,
    > Patrick[/color]

    If your input file looks something like this:
    fred, Barbados/123, Titanic
    sally, Turks/Caicos, Mary Celleste

    then the following code will help you get started:
    <html>
    <head>
    </head>
    <body>
    <?php
    $filename = "test.log";
    if( ! ($fd = fopen($filename , "r")) ) {
    echo "Cannot open $filename for read<br>\n";
    exit;
    }

    while( $line = fgets($fd, 80) ) {
    $values = explode(',', $line); // assumes comma-separated values
    ?>

    <form method="post" action="precmlo g.php">

    <table width="800" cellpadding="5" cellspacing="0" border="0">
    <tr>
    <td width="40%">
    Log sheet completed by:<br> <input class="inform" type="text"
    size="30" name="person" value="<?php echo $values[0]; ?>"><br>
    </td>
    </tr>
    <tr>
    <td>
    Cruise name/number:<br> <input class="inform" type="text"
    size="30" name="cruisenum " value="<?php echo $values[1]; ?>"><br>
    </td>
    </tr>

    <tr>
    <td>
    Ship name:<br> <input class="inform" type="text" size="30"
    name="shipname" value="<?php echo $values[2]; ?>"><br>
    </td>
    </tr>
    </table>

    <input class="button" type="submit" value="Submit">
    </form>


    <?php
    }
    fclose($fd);
    ?>
    </body>
    </html>

    -david-

    Comment

    • Patrick

      #3
      Re: while loops?

      David Haynes wrote:[color=blue]
      > Patrick wrote:
      >[color=green]
      >> I'm trying to while loop using a flat txt file and write the values
      >> out into a form. I have 50-58 values to echo. Am I going to have to
      >> echo each line of html. Or is there a way to make this work between
      >> the php. Right now it wants to write the whole table over and over. Is
      >> my logic screwed up? I am not a programmer as such but can get by. I
      >> have something like this which is an abbreviated version. Any help
      >> would be appreciated.
      >>
      >> Thanks,
      >> Patrick[/color]
      >
      >
      > If your input file looks something like this:
      > fred, Barbados/123, Titanic
      > sally, Turks/Caicos, Mary Celleste
      >
      > then the following code will help you get started:
      > <html>
      > <head>
      > </head>
      > <body>
      > <?php
      > $filename = "test.log";
      > if( ! ($fd = fopen($filename , "r")) ) {
      > echo "Cannot open $filename for read<br>\n";
      > exit;
      > }
      >
      > while( $line = fgets($fd, 80) ) {
      > $values = explode(',', $line); // assumes comma-separated values
      > ?>
      >
      > <form method="post" action="precmlo g.php">
      >
      > <table width="800" cellpadding="5" cellspacing="0" border="0">
      > <tr>
      > <td width="40%">
      > Log sheet completed by:<br> <input class="inform" type="text"
      > size="30" name="person" value="<?php echo $values[0]; ?>"><br>
      > </td>
      > </tr>
      > <tr>
      > <td>
      > Cruise name/number:<br> <input class="inform" type="text" size="30"
      > name="cruisenum " value="<?php echo $values[1]; ?>"><br>
      > </td>
      > </tr>
      >
      > <tr>
      > <td>
      > Ship name:<br> <input class="inform" type="text" size="30"
      > name="shipname" value="<?php echo $values[2]; ?>"><br>
      > </td>
      > </tr>
      > </table>
      >
      > <input class="button" type="submit" value="Submit">
      > </form>
      >
      >
      > <?php
      > }
      > fclose($fd);
      > ?>
      > </body>
      > </html>
      >
      > -david-
      >[/color]
      Hi

      Thanks for the reply. Yes I can already do that ie.
      $values[1]...[2]...[3], and no the file is not comma delimited. It is
      delimited by a new line as some of the values are text area inputs. What
      I want to be able to do is have the while iterate to the next value so
      that any changes in the text file or the originating form that writes
      the text file will not require me to go into this one and change all the
      numbers.

      Patrick

      --
      Patrick A. Smith Assistant System Administrator
      Ocean Circulation Group – USF - College of Marine Science
      http://ocgweb.marine.usf.edu Phone: 727 553-3334

      The trouble with doing something right the first time is that nobody
      appreciates how difficult it was. - La Rochefoucauld

      Comment

      • David Haynes

        #4
        Re: while loops?

        Patrick wrote:[color=blue]
        > David Haynes wrote:[color=green]
        >> Patrick wrote:
        >>[color=darkred]
        >>> I'm trying to while loop using a flat txt file and write the values
        >>> out into a form. I have 50-58 values to echo. Am I going to have to
        >>> echo each line of html. Or is there a way to make this work between
        >>> the php. Right now it wants to write the whole table over and over.
        >>> Is my logic screwed up? I am not a programmer as such but can get by.
        >>> I have something like this which is an abbreviated version. Any help
        >>> would be appreciated.
        >>>
        >>> Thanks,
        >>> Patrick[/color]
        >>
        >>
        >> If your input file looks something like this:
        >> fred, Barbados/123, Titanic
        >> sally, Turks/Caicos, Mary Celleste
        >>
        >> then the following code will help you get started:
        >> <html>
        >> <head>
        >> </head>
        >> <body>
        >> <?php
        >> $filename = "test.log";
        >> if( ! ($fd = fopen($filename , "r")) ) {
        >> echo "Cannot open $filename for read<br>\n";
        >> exit;
        >> }
        >>
        >> while( $line = fgets($fd, 80) ) {
        >> $values = explode(',', $line); // assumes comma-separated values
        >> ?>
        >>
        >> <form method="post" action="precmlo g.php">
        >>
        >> <table width="800" cellpadding="5" cellspacing="0" border="0">
        >> <tr>
        >> <td width="40%">
        >> Log sheet completed by:<br> <input class="inform" type="text"
        >> size="30" name="person" value="<?php echo $values[0]; ?>"><br>
        >> </td>
        >> </tr>
        >> <tr>
        >> <td>
        >> Cruise name/number:<br> <input class="inform" type="text"
        >> size="30" name="cruisenum " value="<?php echo $values[1]; ?>"><br>
        >> </td>
        >> </tr>
        >>
        >> <tr>
        >> <td>
        >> Ship name:<br> <input class="inform" type="text" size="30"
        >> name="shipname" value="<?php echo $values[2]; ?>"><br>
        >> </td>
        >> </tr>
        >> </table>
        >>
        >> <input class="button" type="submit" value="Submit">
        >> </form>
        >>
        >>
        >> <?php
        >> }
        >> fclose($fd);
        >> ?>
        >> </body>
        >> </html>
        >>
        >> -david-
        >>[/color]
        > Hi
        >
        > Thanks for the reply. Yes I can already do that ie.
        > $values[1]...[2]...[3], and no the file is not comma delimited. It is
        > delimited by a new line as some of the values are text area inputs. What
        > I want to be able to do is have the while iterate to the next value so
        > that any changes in the text file or the originating form that writes
        > the text file will not require me to go into this one and change all the
        > numbers.
        >
        > Patrick
        >[/color]
        As long as only your fields are separated by the carriage-returns you
        will be all right. If someone sneaks a carriage-return into the text
        fields, you will lose your synchronization with the input file. (i.e.
        you need to have some trusted form of delimiting the fields)

        If you want to use carriage-returns, just replace the while and explode
        lines with something like:

        while(true) {
        if( ! ($line1 = fgets($fd, 80)) ) break;
        if( ! ($line2 = fgets($fd, 80)) ) break;
        if( ! ($line3 = fgets($fd, 80)) ) break;

        and then $value[0] becomes $line1, etc.

        BTW the 80 can be replaced with whatever your maxlength is for your text
        fields.

        -david-

        Comment

        • Patrick

          #5
          Re: while loops?

          David Haynes wrote:[color=blue]
          > Patrick wrote:
          >[color=green]
          >> David Haynes wrote:
          >>[color=darkred]
          >>> Patrick wrote:
          >>>
          >>>> I'm trying to while loop using a flat txt file and write the values
          >>>> out into a form. I have 50-58 values to echo. Am I going to have to
          >>>> echo each line of html. Or is there a way to make this work between
          >>>> the php. Right now it wants to write the whole table over and over.
          >>>> Is my logic screwed up? I am not a programmer as such but can get
          >>>> by. I have something like this which is an abbreviated version. Any
          >>>> help would be appreciated.
          >>>>
          >>>> Thanks,
          >>>> Patrick
          >>>
          >>>
          >>>
          >>> If your input file looks something like this:
          >>> fred, Barbados/123, Titanic
          >>> sally, Turks/Caicos, Mary Celleste
          >>>
          >>> then the following code will help you get started:
          >>> <html>
          >>> <head>
          >>> </head>
          >>> <body>
          >>> <?php
          >>> $filename = "test.log";
          >>> if( ! ($fd = fopen($filename , "r")) ) {
          >>> echo "Cannot open $filename for read<br>\n";
          >>> exit;
          >>> }
          >>>
          >>> while( $line = fgets($fd, 80) ) {
          >>> $values = explode(',', $line); // assumes comma-separated values
          >>> ?>
          >>>
          >>> <form method="post" action="precmlo g.php">
          >>>
          >>> <table width="800" cellpadding="5" cellspacing="0" border="0">
          >>> <tr>
          >>> <td width="40%">
          >>> Log sheet completed by:<br> <input class="inform" type="text"
          >>> size="30" name="person" value="<?php echo $values[0]; ?>"><br>
          >>> </td>
          >>> </tr>
          >>> <tr>
          >>> <td>
          >>> Cruise name/number:<br> <input class="inform" type="text"
          >>> size="30" name="cruisenum " value="<?php echo $values[1]; ?>"><br>
          >>> </td>
          >>> </tr>
          >>>
          >>> <tr>
          >>> <td>
          >>> Ship name:<br> <input class="inform" type="text" size="30"
          >>> name="shipname" value="<?php echo $values[2]; ?>"><br>
          >>> </td>
          >>> </tr>
          >>> </table>
          >>>
          >>> <input class="button" type="submit" value="Submit">
          >>> </form>
          >>>
          >>>
          >>> <?php
          >>> }
          >>> fclose($fd);
          >>> ?>
          >>> </body>
          >>> </html>
          >>>
          >>> -david-
          >>>[/color]
          >> Hi
          >>
          >> Thanks for the reply. Yes I can already do that ie.
          >> $values[1]...[2]...[3], and no the file is not comma delimited. It is
          >> delimited by a new line as some of the values are text area inputs.
          >> What I want to be able to do is have the while iterate to the next
          >> value so that any changes in the text file or the originating form
          >> that writes the text file will not require me to go into this one and
          >> change all the numbers.
          >>
          >> Patrick
          >>[/color]
          > As long as only your fields are separated by the carriage-returns you
          > will be all right. If someone sneaks a carriage-return into the text
          > fields, you will lose your synchronization with the input file. (i.e.
          > you need to have some trusted form of delimiting the fields)
          >
          > If you want to use carriage-returns, just replace the while and explode
          > lines with something like:
          >
          > while(true) {
          > if( ! ($line1 = fgets($fd, 80)) ) break;
          > if( ! ($line2 = fgets($fd, 80)) ) break;
          > if( ! ($line3 = fgets($fd, 80)) ) break;
          >
          > and then $value[0] becomes $line1, etc.
          >
          > BTW the 80 can be replaced with whatever your maxlength is for your text
          > fields.
          >
          > -david-
          >[/color]

          Thanks, I will give that a try.

          Patrick

          --
          Patrick A. Smith Assistant System Administrator
          Ocean Circulation Group – USF - College of Marine Science
          http://ocgweb.marine.usf.edu Phone: 727 553-3334

          The trouble with doing something right the first time is that nobody
          appreciates how difficult it was. - La Rochefoucauld

          Comment

          • Kimmo Laine

            #6
            Re: while loops?

            "Patrick" <psmith@marine. usf.edu> wrote in message
            news:e5hmne$l9v $1@news1.usf.ed u...[color=blue]
            > I'm trying to while loop using a flat txt file and write the values out
            > into a form. I have 50-58 values to echo. Am I going to have to echo each
            > line of html. Or is there a way to make this work between the php. Right
            > now it wants to write the whole table over and over. Is my logic screwed
            > up? I am not a programmer as such but can get by. I have something like
            > this which is an abbreviated version. Any help would be appreciated.
            >
            > Thanks,
            > Patrick
            >
            > <html>
            > <head>
            > <?
            > $filename = "C10AB_precruis e.log";
            > $larray = file($filename) ;
            > ?>
            > </head>
            > <body>
            > <?
            > $i=0;
            > while ($i <= 60)
            > {
            > ?>
            >
            > <form method="post" action="precmlo g.php">
            >
            > <table width="800" cellpadding="5" cellspacing="0" border="0">
            > <tr>
            > <td width="40%">
            > Log sheet completed by:<br> <input class="inform" type="text" size="30"
            > name="person" value="<?php echo $larray[$i]; ?>"><br>
            > </td>
            > </tr>
            > <tr>
            > <td>
            > Cruise name/number:<br> <input class="inform" type="text" size="30"
            > name="cruisenum " value="<?php echo $larray[$i]; ?>"><br>
            > </td>
            > </tr>
            >
            > <tr>
            > <td>
            > Ship name:<br> <input class="inform" type="text" size="30"
            > name="shipname" value="<?php echo $larray[$i]; ?>"><br>
            > </td>
            > </tr>
            > </table>
            >
            > <input class="button" type="submit" value="Submit">
            > </form>
            >
            >
            > <?
            > }
            > $i++;
            > ?>
            > </body>
            > </html>
            >[/color]

            $i++ should be INSIDE the while loop to have effect. Now you don't increase
            it so it keeps writing the same form over an over again because $i gets no
            bigger and therefore it's always less than 60 thus it becomes an infinite
            loop.

            fix
            <?
            } // Loop stops here, $i is never reached.
            $i++;
            ?>

            to

            <?
            $i++; // Now it's inside the loop and is incremented
            }
            ?>

            Or even better, use a for-loop instead. That way you'll do all the necessary
            loop-control in one logical place:

            for($i=0; $i<=60; $i++) { ....

            HTH

            --
            "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
            spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


            Comment

            Working...