Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing txt file, comparison errors

    I'm am using the following code:
    PHP 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
    Last edited by skufft; 03-24-2004 at 07:47 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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....ing.conversion

    Try this perhaps:
    PHP Code:
    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 Code:
    <?php
    echo "\$matches[$i][3]==$matches[$i][3]; type is " gettype ($matches[$i][3]) . "<br />\n";
    ?>
    Hope that helps,
    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What you said definitely made something in my mind click, and I added the following:
    PHP Code:
    $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.

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •