Connecting Tech Pros Worldwide Help | Site Map

reading the correct line sequence from a txt file

 
LinkBack Thread Tools Search this Thread
  #1  
Old April 10th, 2007, 07:55 AM
programming
Guest
 
Posts: n/a
Default reading the correct line sequence from a txt file

Is there any reason why i am NOT reading each invididual line from a
txt file in this script...e.g i keep on getting 2 lines printed outed
when there are clearly
four lines in the txt file.

also, is my counter right here?

<? php
$listf = fopen ("./member.txt", "r");
# read the file
$line = fgets($listf, 4096);


while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
$count++;
$duserid=trim($data[0]);
$dname=trim($data[1]);



list ($duserid,$dname,$actions) = split("\|",$line);
$line = chop($line); //chops space out of program

$actions= "<a href=\"./delete_member.php?record_id=29\">Delete</a|
<a href=\"./edit_member.php?record_id=29\">Edit</a></td>";
$data = fgetcsv($listf, 1024);
echo "<tr><td>$duserid</td><td>$dname</td><td>$actions<td></tr>";

}

fclose($listf);
?>


  #2  
Old April 10th, 2007, 09:15 AM
Toby A Inkster
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file

programming wrote:
Quote:
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
[...]
Quote:
$data = fgetcsv($listf, 1024);
I'm not very familiar with fgetcsv() but you appear to be reading two
lines for each iteration. Which means that if the file has "n" lines,
you'll only end up with "n/2" iterations of the while loop.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
  #3  
Old April 10th, 2007, 10:05 AM
programming
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file

Ok well your suggestion has sort of fixed the problem but know it is
like reading
every 3 of 4 lines, instead of 2 of 4 lines.

<?php
$listf = fopen ("./member.txt", "r");
# read the file
$line = fgets($listf, 1024);

while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
$count++;
$duserid=trim($data[0]);
$dname=trim($data[1]);

list ($duserid,$dname,$actions) = split("\|",$line);
$line = chop($line); //chops space out of program

$actions= "<a href=\"./delete_member.php?
record_id=29\">Delete</a| <a href=\"./edit_member.php?
record_id=29\">Edit</a></td>";
//$data = fgetcsv($listf, 1024);
echo "<tr><td>$duserid</td><td>$dname</td><td>
$actions<td></tr>";

}

fclose($listf);
?>

may be a do while loop might fix the problem....





On Apr 10, 7:13 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Quote:
programming wrote:
Quote:
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
[...]
Quote:
$data = fgetcsv($listf, 1024);
>
I'm not very familiar with fgetcsv() but you appear to be reading two
lines for each iteration. Which means that if the file has "n" lines,
you'll only end up with "n/2" iterations of the while loop.
>
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
>
* = I'm getting there!

  #4  
Old April 10th, 2007, 10:45 AM
programming
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file


Big problem now it appears that
it appears to be copying the 1st line
of the txt file to web page 3 times...


On Apr 10, 7:13 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Quote:
programming wrote:
Quote:
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
[...]
Quote:
$data = fgetcsv($listf, 1024);
>
I'm not very familiar with fgetcsv() but you appear to be reading two
lines for each iteration. Which means that if the file has "n" lines,
you'll only end up with "n/2" iterations of the while loop.
>
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
>
* = I'm getting there!

  #5  
Old April 10th, 2007, 10:45 AM
Rami Elomaa
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file

programming kirjoitti:
Quote:
Ok well your suggestion has sort of fixed the problem but know it is
like reading
every 3 of 4 lines, instead of 2 of 4 lines.
>
<?php
$listf = fopen ("./member.txt", "r");
# read the file
$line = fgets($listf, 1024);
The file is read here the first time, and the filepointer moves from row
1 to row 2. the data is however discared.
Quote:
>
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
Now it starts reading the file from the second line, and so on... That's
why the first line is not printed.

Just remove the extra fgets from the beginning.

--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
  #6  
Old April 10th, 2007, 10:45 AM
Rami Elomaa
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file

programming kirjoitti:
Quote:
Big problem now it appears that
it appears to be copying the 1st line
of the txt file to web page 3 times...
>
You read the first line to $line with fgets, and the next lines to $data
in the while-condition. Inside the loop you use $line.

Stop being so stupid!

--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
  #7  
Old April 10th, 2007, 11:05 AM
Toby A Inkster
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file

programming wrote:
Quote:
$line = fgets($listf, 1024);
WTF is this line doing?

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
  #8  
Old April 10th, 2007, 11:15 AM
Rami Elomaa
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file

Toby A Inkster kirjoitti:
Quote:
programming wrote:
>
Quote:
>$line = fgets($listf, 1024);
>
WTF is this line doing?
>
It's causing all the trouble he's been having. :)

--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
  #9  
Old April 10th, 2007, 11:55 AM
Jerry Stuckle
Guest
 
Posts: n/a
Default Re: reading the correct line sequence from a txt file

programming wrote:
Quote:
Ok well your suggestion has sort of fixed the problem but know it is
like reading
every 3 of 4 lines, instead of 2 of 4 lines.
>
<?php
$listf = fopen ("./member.txt", "r");
# read the file
$line = fgets($listf, 1024);
>
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
$count++;
$duserid=trim($data[0]);
$dname=trim($data[1]);
>
list ($duserid,$dname,$actions) = split("\|",$line);
$line = chop($line); //chops space out of program
>
$actions= "<a href=\"./delete_member.php?
record_id=29\">Delete</a| <a href=\"./edit_member.php?
record_id=29\">Edit</a></td>";
//$data = fgetcsv($listf, 1024);
echo "<tr><td>$duserid</td><td>$dname</td><td>
$actions<td></tr>";
>
}
>
fclose($listf);
?>
>
may be a do while loop might fix the problem....
>
>
>
>
>
On Apr 10, 7:13 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Quote:
>programming wrote:
Quote:
>>while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
>>{
>[...]
Quote:
>>$data = fgetcsv($listf, 1024);
>I'm not very familiar with fgetcsv() but you appear to be reading two
>lines for each iteration. Which means that if the file has "n" lines,
>you'll only end up with "n/2" iterations of the while loop.
>>
>--
>Toby A Inkster BSc (Hons) ARCS
>Contact Me ~http://tobyinkster.co.uk/contact
>Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
>>
>* = I'm getting there!
>
>
You're also getting the first right after you open the file and throw it
away.

Did you do ANY debugging on this yourself? Do you have ANY idea what
the functions you're using do?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.