...

View Full Version : Parsing txt file, comparison errors



skufft
03-24-2004, 06:28 PM
I'm am using the following code:


<?php
$file = fopen('matchlist.txt', 'r');
while (!feof($file)):
$info = fgets($file);
$matches[] = explode(" ", $info);
endwhile;

$wins = 0;
$losses = 0;
$ties = 0;
for ($i = 0; $i < count($matches); $i++):
echo '<tr onMouseOver="this.bgColor=\'#CDDCE7\'" onMouseOut="this.bgColor=\'\'">
<td align="center">' . $matches[$i][0] . '</td>
<td align="center">' . str_replace("_", " ", $matches[$i][1]) . '</td>
<td align="center">' . str_replace("_", " ", $matches[$i][2]) . '</td>
<td align="center">' . $matches[$i][3] . ' - ' . $matches[$i][4] . '</td>
<td align="center">';
if ($matches[$i][3] > $matches[$i][4]):
echo '<span class="style7">W</span>';
$wins++;
elseif ($matches[$i][3] < $matches[$i][4]):


echo '<span class="style6">L</span>';
$losses++;
else:
echo '<span class="style8">T</span>';
$ties++;
endif;
echo '</td>
</tr>';
endfor;
?>

to parse this exact test file:


2004/02/10 Slow_in_the_Head CAL-O 10 14
2004/02/11 spec7 UGS-O 19 11
2004/02/17 Team_MeeK CAL-O 20 4
2004/02/18 Majestic12 UGS-O 16 0
2004/02/24 NiTe CAL-O 23 1
2004/02/25 Holy_Elite UGS-O 24 16
2004/03/02 Zero_Ping_Gaming CAL-O 22 2
2004/03/03 LGA UGS-O 16 0
2004/03/09 Slick_Assasins CAL-O 20 4
2004/03/10 Army_of_Assasins UGS-O 26 4
2004/03/16 defiance.inc CAL-O 7 17
2004/03/17 scythe UGS-O 6 24
2004/03/20 insufficient_evidence LAN_-_DLF 13 7
2004/03/20 Lan_Clan_Ohio LAN_-_DLF 16 12
2004/03/20 caffeine LAN_-_DLF 13 1
2004/03/20 Lan_Clan_Ohio LAN_-_DLF 13 8

into this webpage: http://enjoy.childsale.net/matches.php

If you look closely at the page, it is declaring a couple of the wins as losses, and I just can't figure out why. Anyone have any ideas?

Note: In the php file, in the echo statement, I did put backslashes to exit the single quotes, but it didn't show up when I posted this thread.

Hmm..I'm now thinking this may be related to this bug.. http://bugs.php.net/bug.php?id=25671 as I am running php 4.3.3

sad69
03-24-2004, 08:20 PM
I'm not for sure, but it may be that because you've read in those values from file, that PHP is interpreting your scores as strings, not numbers.

This may be of assistance:
http://www.zend.com/manual/language.types.string.php#language.types.string.conversion

Try this perhaps:


if ( ($matches[$i][3]*1) > ($matches[$i][4]*1) ):
echo '<span class="style7">W</span>';
$wins++;
elseif ( ($matches[$i][3]*1) < ($matches[$i][4]*1) ):
echo '<span class="style6">L</span>';
$losses++;
else:
echo '<span class="style8">T</span>';
$ties++;
endif;


That should hopefully convert your strings to numbers.. if anyone else has any ideas please post.

PS: This could help with debugging?


<?php
echo "\$matches[$i][3]==$matches[$i][3]; type is " . gettype ($matches[$i][3]) . "<br />\n";
?>


Hope that helps,
Sadiq.

skufft
03-24-2004, 08:36 PM
What you said definitely made something in my mind click, and I added the following:


$matches[$i][3] = intval($matches[$i][3]);
$matches[$i][4] = intval($matches[$i][4]);

Right after the for loop began.

Thanks a ton Sadiq, I've been sitting on this for over an hour or two.

sad69
03-24-2004, 10:02 PM
And thank you! I know from Javascript there's a parseInt(), but I couldn't think of it in PHP!

So we'll both remember that!

Ciao,
Sadiq.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum