Results 1 to 4 of 4

Thread: colon problem

  1. #1
    Join Date
    Oct 2007
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default colon problem

    I get this ; coming up on my page for each entry that is added.

    I'm using mySQL and the php select code is:

    Code:
    <?php
    $count = 0;
    foreach ( $user as $item )
    		{
    if ($result[$count] == "Win")
    {
    $result3 = "class=\"leftcolgreen\"";
    }
    if ($result[$count] == "Loss")
    {
    $result3 = "class=\"leftcolred\"";
    }
    if ($result[$count] == "Draw")
    {
    $result3 = "class=\"leftcolyellow\"";
    }
    echo "<tr>";
    echo "<td class=\"leftcol1\">";
    echo $day[$count];
    echo ".";
    echo $month[$count];
    echo ".";
    echo $year[$count];
    echo "<td class=\"leftcol2\"><img src=\"./images/flags/au.gif\" width=\"18\" height=\"12\"></td>";
    echo "<td class=\"leftcol3\">";
    echo $user[$count];
    echo "</td>;";
    echo "<td class=\"leftcol4\">";
    echo $ladder[$count];
    echo "</td>";
    echo "<td ";
    echo $result3;
    echo ">";
    echo $result[$count];
    echo " ";
    echo $score1[$count];
    echo "-";
    echo $score2[$count];
    echo "</td>";
    echo "<td class=\"leftcol6\"><a href=\"selectmatch.php?id=$id[$count]\"><img src=\"./images/profile.gif\" width=\"11\"></a>";
    echo "</tr>";
    $count++;
    		} 
    ?>
    Please help.
    Last edited by magik; 01-28-2008 at 03:31 AM.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Ugh, escape from PHP before outputting HTML and use single quotes for faster parsing:

    Code:
    <?php
    $count = 0;
    foreach ($user as $item) {
     if ($result[$count] == "Win") {
       $result3 = 'class="leftcolgreen"';
     }
    
     if ($result[$count] == "Loss") {
       $result3 = 'class="leftcolred"';
     }
    
     if ($result[$count] == "Draw") {
       $result3 = 'class="leftcolyellow"';
     }
    ?>
     <tr>
      <td class="leftcol1">
      <?php echo $day[$count]; ?>.<?php echo $month[$count];?>.<?php echo $year[$count];?></td>
      <td class="leftcol2"><img src="./images/flags/au.gif" width="18" height="12"></td>
      <td class="leftcol3"><?php echo $user[$count]; ?></td>
      <td class="leftcol4"><?php echo $ladder[$count]; ?></td>
      <td <?php echo $result3;?>><?php echo $result[$count]; ?> <?php echo $score1[$count]; ?> - <?php echo $score2[$count]; ?></td>
      <td class="leftcol6"><a href="selectmatch.php?id=<?php echo $id[$count];?>"><img src="./images/profile.gif" width="11"></a></td>
     </tr>
    <?php 
     $count++;
    } 
    ?>
    but if you are just looking for where the semicolon is getting inserted at, take a look at the highlight below.

    Code:
    <?php
    $count = 0;
    foreach ( $user as $item )
    		{
    if ($result[$count] == "Win")
    {
    $result3 = "class=\"leftcolgreen\"";
    }
    if ($result[$count] == "Loss")
    {
    $result3 = "class=\"leftcolred\"";
    }
    if ($result[$count] == "Draw")
    {
    $result3 = "class=\"leftcolyellow\"";
    }
    echo "<tr>";
    echo "<td class=\"leftcol1\">";
    echo $day[$count];
    echo ".";
    echo $month[$count];
    echo ".";
    echo $year[$count];
    echo "<td class=\"leftcol2\"><img src=\"./images/flags/au.gif\" width=\"18\" height=\"12\"></td>";
    echo "<td class=\"leftcol3\">";
    echo $user[$count];
    echo "</td>;";
    echo "<td class=\"leftcol4\">";
    echo $ladder[$count];
    echo "</td>";
    echo "<td ";
    echo $result3;
    echo ">";
    echo $result[$count];
    echo " ";
    echo $score1[$count];
    echo "-";
    echo $score2[$count];
    echo "</td>";
    echo "<td class=\"leftcol6\"><a href=\"selectmatch.php?id=$id[$count]\"><img src=\"./images/profile.gif\" width=\"11\"></a>";
    echo "</tr>";
    $count++;
    		} 
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Oct 2007
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks very much!

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
     if ($result[$count] == "Win") {
       $result3 = 'class="leftcolgreen"';
     }
    
     if ($result[$count] == "Loss") {
       $result3 = 'class="leftcolred"';
     }
    
     if ($result[$count] == "Draw") {
       $result3 = 'class="leftcolyellow"';
     }
    Better would be an associative array:
    Code:
    <?php
      for ($count = 0; isset($user[$count]) && $item = $user[$count]; ++$count) {
        $class = array('Win'  => 'leftcolgreen',
                       'Loss' => 'leftcolred',
                       'Draw' => 'leftcolyellow');
        $class = $class[$result[$count]];
        // Two assignments because PHP only supports indexing
        // on variables.  Ugh!
    ?>
      <tr>
        <td class="leftcol1">
          <?php echo implode('.', array($day[$count], $month[$count], $year[$count])); ?>
        </td>
        <td class="leftcol2">
          <img src="images/flags/au.gif" width="18" height="12">
        </td>
        <td class="leftcol3">
          <?php echo $user[$count]; ?>
        </td>
        <td class="leftcol4">
          <?php echo $ladder[$count]; ?>
        </td>
        <td class="<?php echo $class; ?>">
          <?php echo $result[$count]; ?>
          <?php echo $score1[$count]; ?>
          - <?php echo $score2[$count]; ?>
        </td>
        <td class="leftcol6">
          <a href="selectmatch.php?id=<?php echo $id[$count]; ?>">
            <img src="./images/profile.gif" width="11">
          </a>
        </td>
      </tr>
    <?php } ?>
    As you can see, this all gets a bit cumbersome, which is why a good templating system can come in handy. You've also got the "parallel arrays" pattern that suggests you should be using either a single array of associative arrays, or a class and an array of instances. Your CSS classes should really be named semantically, not presentationally ("leftcol-loss", "leftcol-draw", "leftcol-win" for example).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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