Forum Moderators: coopster

Message Too Old, No Replies

Problem filtering a tabbed delimited file

         

fintan

12:07 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



Hi I'm trying to filter a tabbed delimited file so when a user enters a phone number they will receive their bill for the month. I can get the first number but cant get any others. I know I have to do a loop though the file but I'm not sure were. Here's what I got

$fname = fopen("phones.csv", "r") or die("Couldn't open file");
$line = fgets($fname);
list($num, $sum) = split(" ", $line, 2);

for($i=0; $i<$num; $i++){
if($number == $num){
echo "<br><br>The Cost of <strong>$number</strong> in <strong>$month</strong> was<br><br> <strong>€ $sum</strong>";}
else{echo "<br><br><br>Sorry the number was not found.";}
}

fclose($fname);

Where $number & $month is from the users query.
Any ideas thanks

fintan.

coopster

1:40 pm on Aug 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<edited> Hold on, misread your post, you said you ARE getting the first row correctly, it's after that you are having difficulties... <edited>

[edited by: coopster at 2:21 pm (utc) on Aug. 22, 2003]

coopster

2:20 pm on Aug 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




<?php
$fname= fopen("phones.csv", "r") or die("Couldn't open file");
while (!feof ($fname)) {
$line = fgets($fname, 4096); # row length up to 4KB
list($num, $sum) = split(" ", $line, 2);
if($number == $num) {
echo "<br><br>The Cost of <strong>$number</strong> in <strong>$month</strong> was<br><br> <strong>€ $sum</strong>";
} else {
echo "<br><br><br>Sorry the number was not found.";
}
}
fclose ($fname);
?>

[edited by: jatar_k at 4:56 pm (utc) on Aug. 22, 2003]
[edit reason] fixed sidescroll [/edit]

fintan

3:42 pm on Aug 22, 2003 (gmt 0)

10+ Year Member



Thanks for the input but

Sorry the number was not found.

Sorry the number was not found.

The Cost of 12955564 in July was

€ 60.73

Sorry the number was not found.

If I put another if statement in there saying if $number = null don't echo it. Would that work?
Thanks

coopster

4:24 pm on Aug 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oh, you only have one row for each customer and once found, exit the loop? Try again...

<?php
$fname= fopen("phones.csv", "r") or die("Couldn't open file");
while (!feof ($fname)) {
$line = fgets($fname, 4096); # row length up to 4KB
list($num, $sum) = split(" ", $line, 2);
if($number == $num) {
echo "<br><br>The Cost of <strong>$number</strong> in <strong>$month</strong> was<br><br> <strong>€ $sum</strong>";
break;
}
}
if ($number!= $num) echo "<br><br><br>Sorry the number was not found.";
fclose ($fname);
?>

[edited by: coopster at 4:30 pm (utc) on Aug. 22, 2003]

[edited by: jatar_k at 4:56 pm (utc) on Aug. 22, 2003]
[edit reason] fixed sidscroll [/edit]

coopster

4:29 pm on Aug 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sorry about the confusion ;)

fintan

9:35 am on Aug 25, 2003 (gmt 0)

10+ Year Member



Thanks for your help

fintan.

fintan

11:16 am on Aug 25, 2003 (gmt 0)

10+ Year Member



Just one more thing in the while loop there's a variable called $num, why can't I use that someplace else. Say in an if statement later on.

coopster

3:07 pm on Aug 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can. Unless you have this code snippet inside a function in which case $num will be a local variable to the function itself.