View Full Version : colon problem
magik
01-28-2008, 03:13 AM
I get this ; coming up on my page for each entry that is added.
I'm using mySQL and the php select code is:
<?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.
thetestingsite
01-28-2008, 03:27 AM
Ugh, escape from PHP before outputting HTML and use single quotes for faster parsing:
<?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.
<?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.
magik
01-28-2008, 03:29 AM
Thanks very much!
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:
<?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 (http://smarty.php.net/) 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).
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.