Forum Moderators: coopster
My problem is that i am developing a match system for my new content management system. I have reached the stage of displaying the 2 teams scores. However i would like to display the winning sides score in the color green and the losing sides in red, with draws being displayed in white.
After 3 hours today i thought i had the solution as i managed to get the correct color output on one display. However upon adding further data it appears it has not worked.
Here is the code i have at the moment.
<?php
$dbhost = '?';
$dbuser = '?_?';
$dbpass = '?';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = '?';
mysql_select_db($dbname);
$result = mysql_query("SELECT squad_finalscore FROM matches ORDER BY match_id DESC LIMIT 1");
$result2 = mysql_query("SELECT opponent_finalscore FROM matches ORDER BY match_id DESC LIMIT 1");
$row = mysql_fetch_assoc($result);
if ($result > $result2) {
echo "<font color='#39c127'>$row[squad_finalscore]</font>";
} elseif ($result < $result2) {
echo "<font color='#e12929'>$row[squad_finalscore]</font>";
} else {
echo "No, $test must be less than 40 and 35 and 30.";
}
mysql_close($conn);
?>
</div>
<div id="apDiv7">
<?php
$dbhost = '?';
$dbuser = '?_?';
$dbpass = '?';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = '?';
mysql_select_db($dbname);
$result = mysql_query("SELECT opponent_finalscore FROM matches ORDER BY match_id DESC LIMIT 1");
$result2 = mysql_query("SELECT squad_finalscore FROM matches ORDER BY match_id DESC LIMIT 1");
$row = mysql_fetch_assoc($result);
if ((int)$result > $result2) {
echo "<font color='#39c127'>$row[opponent_finalscore]</font>";
} elseif ((int)$result < $result2) {
echo "<font color='#e12929'>$row[opponent_finalscore]</font>";
} else {
echo "No, $test must be less than 40 and 35 and 30.";
}
mysql_close($conn);
?>
For the life of me i cannot see why this wont work, but like i stated earlier i have a low knowledge of PHP compared to others.
Any input or help or fixes! would be very much appreciated.
Thanks in advance for your time,
Regards,
philEHHH
echo "No, $test must be less than 40 and 35 and 30."
Don't be echoing code and text together.
Example: say $white=FFFFFF
echo "The output is $white" would echo The output is $white
echo "The output is ".$white would echo The output is FFFFFF
Correct Method:
echo "No, ".$test." must be less than 40 and 35 and 30.";
[edited by: StoutFiles at 5:57 pm (utc) on May 30, 2008]
$row = mysql_fetch_assoc($result);
if ($result > $result2) {
You're comparing the query resources. You need to fetch the row from your second query and compare those.
$row2 = mysql_fetch_assoc($result2);
if($row['squad_finalscore'] > $row2['opponent_finalscore'])
To reference array variables in quoted text, surround them with curly braces, and you really should surround alpha array indices with quotes:
if($row['squad_finalscore'] > $row2['opponent_finalscore']) {
echo "<font color='#39c127'>{$row['squad_finalscore']}</font>";
StoutFiles:
Don't be echoing code and text together.There's nothing wrong with that.
$row = mysql_fetch_assoc($result);
if($row['squad_finalscore'] > $row['opponent_finalscore'])
After all that great help before iv run into a spot of bother on a similair area. The colors are displaying fine for the match results however only one result is now showing instead of 3. I think something in the code is stopping further entries from being displayed.
<?php
$dbhost = '?';
$dbuser = '?_?';
$dbpass = '?';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = '?';
mysql_select_db($dbname);
$result = mysql_query("SELECT * FROM matches ORDER BY match_id DESC LIMIT 6");
$row = mysql_fetch_assoc($result);
{
echo "<img src='" . $row['game_flag'] . "' align='top' />" . "<img src='" . $row['opponent_flag'] . "' align='top' />" . $row['opponent_name'] . " ";
}
if($row['squad_finalscore'] > $row['opponent_finalscore']) {
echo "<font color='#39c127'>{$row['squad_finalscore']}-{$row['opponent_finalscore']}</font>";
} elseif ($row['squad_finalscore'] < $row['opponent_finalscore']) {
echo "<font color='#e12929'>{$row['squad_finalscore']}-{$row['opponent_finalscore']}</font>";
} else {
echo "<font color='#FFFFFF'>{$row['squad_finalscore']}-{$row['opponent_finalscore']}<br></font>";
}
mysql_close($conn);
?>
So imagine this is how the outputs should be...
H2K 21-15
eSuba 20-10
EGAMING 13-1
Its only displaying
H2K 21-15
Again, thanks in advance!
// This line retrieves 1 single row of the selection.
$row = mysql_fetch_assoc($result);
To retrieve multiple rows, you need to set up a control loop:
for($i = 0; $i < 6; $i++) {
$row = mysql_fetch_assoc($result);
// down through the echo
} // EndFor retrieve records
Using a numbered loop like that presupposes that you will always get the 6 records. If you get less than 6, you'll generate errors, so the better way (IMO) is to leave it open-ended:
while($row = mysql_fetch_assoc($result)) {
// down through the echo
} // EndWhile retrieve records
The reason that one works is because when there are no more records left, mysql_fetch_assoc() returns FALSE so you drop out of the loop.
I still say you should write it that way as it's easier...
That's what it really comes down to, whatever is easier for each individual to understand. Personally the extra quotes and concatenation operators confound my eyeballs so I like to see it 'all at once' but that's just me - we all develop our own styles as we go along. If it works, IMO, it's 'right'. One of the things I like about PHP is that there's almost always 3+ different ways to do a thing so we each get to choose the path we travel to all arrive at the same ?>.