473,386 Members | 1,803 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,386 software developers and data experts.

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

Apr 10 '07 #1
8 2635
programming wrote:
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
[...]
$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!
Apr 10 '07 #2
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:
programming wrote:
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
[...]
$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!

Apr 10 '07 #3

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:
programming wrote:
while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
[...]
$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!

Apr 10 '07 #4
programming kirjoitti:
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.
>
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.

--
Ra*********@gmail.com
"Olemme apinoiden planeetalla."
Apr 10 '07 #5
programming kirjoitti:
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!

--
Ra*********@gmail.com
"Olemme apinoiden planeetalla."
Apr 10 '07 #6
programming wrote:
$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!
Apr 10 '07 #7
Toby A Inkster kirjoitti:
programming wrote:
>$line = fgets($listf, 1024);

WTF is this line doing?
It's causing all the trouble he's been having. :)

--
Ra*********@gmail.com
"Olemme apinoiden planeetalla."
Apr 10 '07 #8
programming wrote:
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:
>programming wrote:
>>while(($data = fgetcsv($listf,1024,"|"))!=FALSE)
{
[...]
>>$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.
js*******@attglobal.net
==================
Apr 10 '07 #9

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

Similar topics

2
by: Thomas Philips | last post by:
In the course of playing around with file input and output, I came across some behavior that is not quite intuitive. I created a simple text file, test.txt, which contains only 3 lines, and which I...
11
by: W. Cerven | last post by:
I have a list of n inputs of varying type I wish to read from a file: For example: string string int int double double int int int double ... My input file gives its values on a single line,...
7
by: John Bowman | last post by:
Hi All, I'm fairly new to XML, so I presume I'm doing something obviously dense here, but here it goes. Below is an XML file that was extracted from a test Windows Installer .MSP (patch) file...
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
5
by: aboxylica | last post by:
I have a file which contains something like this >ref|NC_001133| CCACACCACACCCACACACCCACACACCACACCACACACCACACCACACCCACACACACA CATCCTAACACTACCCTAACACAGCCCTAATCTAACCCTGGCCAACCTGTCTCTCAACTT...
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
9
by: antar2 | last post by:
I am a starter in python and would like to write a program that reads lines starting with a line that contains a certain word. For example the program starts reading the program when a line is...
13
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
If you have: #define errno retrieve_errno_func() #define SUBSYSTEM_INCLUDE(subsystem, file) <subsystem/include/file> then what should happen when you do:
4
by: zr | last post by:
Hi, i need to read a text file which contains a list of items, all of type ItemType, separated by whitespace and newlines. For each line, there will be a vector<ItemTypeobject that will store...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.