473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_precruise.log";
$larray = file($filename);
?>
</head>
<body>
<?
$i=0;
while ($i <= 60)
{
?>

<form method="post" action="precmlog.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
May 30 '06 #1
5 1500
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="precmlog.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-

May 30 '06 #2
David Haynes wrote:
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="precmlog.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-

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

May 30 '06 #3
Patrick wrote:
David Haynes wrote:
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="precmlog.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-

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

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-

May 30 '06 #4
David Haynes wrote:
Patrick wrote:
David Haynes wrote:
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="precmlog.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-

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

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-


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

May 30 '06 #5
"Patrick" <ps****@marine.usf.edu> wrote in message
news:e5**********@news1.usf.edu...
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_precruise.log";
$larray = file($filename);
?>
</head>
<body>
<?
$i=0;
while ($i <= 60)
{
?>

<form method="post" action="precmlog.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>


$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
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
May 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marco | last post by:
We can read this comment at php.net >jon >05-Nov-2003 10:06 >According to my tests, the "do...while" control structure actually seems to be ~40% faster than the "for" control structure. At...
24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
3
by: RobG | last post by:
A little while ago I opined that do/while loops are harder to read than for loops, and therefore I preferred using for loops. However, it was pointed out that do/while has significant...
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
8
by: jvb | last post by:
Hey all, I figure it's Wednesday, why not put a question up for debate. Beyond personal preference, is there any benefit (performance or otherwise) to using one loop over the other? For example,...
6
by: kydavis77 | last post by:
i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 Write a program that continually reads in numbers...
2
by: Sorin Schwimmer | last post by:
I am thinking on something in the following form: <code> import time import thread delay=True def fn() global delay
3
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text...
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.