Page 1 of 1
why this switch statement does not work
Posted: Fri Feb 11, 2005 7:16 pm
by HuntHerr
I have the following function that is supposed to change the color what is displayed; whether being win, tie, or loss. For some reason it is not displaying them correctly, havent figured out the pattern yet =\ - any ideas?
Code: Select all
function recentmatchresult()
{
$query = "SELECT final FROM matches ORDER BY id DESC LIMIT 0, 5";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
switch ($rowї'final'])
{
case "WIN":
echo '<font color="green">';
break;
case "TIE":
echo '<font color="blue">';
break;
case "LOSS":
echo '<font color="#990000">';
break;
}
$res .= "".$rowї'final']."<br>";
}
echo $res;
}
Posted: Fri Feb 11, 2005 7:24 pm
by Chris Corbyn
I can't see any problem with the function.... what happens when you use it? Where does $res get defined? Do you have a bigger slice of code?
Posted: Fri Feb 11, 2005 7:42 pm
by feyd
your echo'ing the colors, yet concatenating the value you are coloring?
Posted: Fri Feb 11, 2005 11:12 pm
by HuntHerr
on the admin side which i put it:
Code: Select all
<form action="insert.php" method="post" >
<table width="400" border="0" cellspacing="2" cellpadding="2">
<tr>
<td valign="middle"><h2>Date of Match</h2></td>
<td><input name="matchdate" type="text" maxlength="8"></td>
</tr>
<tr>
<td valign="middle"><h2>Oppopent</h2></td>
<td><input name="opponent" type="text" maxlength="25"></td>
</tr>
<tr>
<td valign="middle"><h2>League/Event</h2></td>
<td><input name="league" type="text" maxlength="25"></td>
</tr>
<tr>
<td valign="middle"><h2>Map</h2></td>
<td><input name="map" type="text" id="map" maxlength="25"></td>
</tr>
<tr>
<td valign="middle"><h2>Score</h2></td>
<td><input name="score" type="text" maxlength="5"></td>
</tr>
<tr>
<td valign="middle"><h2>Result</h2></td>
<td><select name="final" size="1" id="final">
<option class="win">WIN</font></option>
<option class="tie">TIE</option>
<option class="loss">LOSS</option>
</select></td>
</tr>
<tr>
<td><div align="right"></div></td>
<td> <input type="Submit"></td>
</tr>
</table>
</form>
and then the insert page where it inserts in db
Code: Select all
<?
include("config.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO matches VALUES ('','$matchdate','$opponent','$league','$map','$score','$final');";
echo "Match Added: ";
$q = mysql_query($query) or die ("<font face=990000>Failed</font>");
echo "<font color=003366>Successful</font><br>";
?>
Posted: Fri Feb 11, 2005 11:18 pm
by feyd
did you read and understand my post? .. I said what the problem is..
Posted: Fri Feb 11, 2005 11:41 pm
by HuntHerr
did not understand it, sorry =\
Posted: Fri Feb 11, 2005 11:47 pm
by feyd
okay.. here's your originally posted code, which I added some comments to:
Code: Select all
function recentmatchresult()
{
$query = "SELECT final FROM matches ORDER BY id DESC LIMIT 0, 5";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
switch ($rowї'final'])
{
case "WIN":
echo '<font color="green">'; // <-- echo line
break;
case "TIE":
echo '<font color="blue">'; // <-- echo line
break;
case "LOSS":
echo '<font color="#990000">'; // <-- echo line
break;
}
$res .= "".$rowї'final']."<br>"; // <-- concat line
}
echo $res;
}
The first three commented lines are echo lines. They are echoing the font color out. The last commented line concatenates (joins together) the switched upon data only. The font coloring is sent elsewhere.
Posted: Fri Feb 11, 2005 11:53 pm
by HuntHerr
Okay, so do away with the echo? I am a little confused and its late =\
Posted: Fri Feb 11, 2005 11:59 pm
by feyd
I believe you need to concat the font color with $res..
Posted: Sat Feb 12, 2005 12:04 am
by HuntHerr
Sorry, am not used to this term, concat
Posted: Sat Feb 12, 2005 12:08 am
by feyd
concat, short for concatenate ::
dictionary.com wrote:con·cat·e·nate
tr.v. con·cat·e·nat·ed, con·cat·e·nat·ing, con·cat·e·nates
1. To connect or link in a series or chain.
2. Computer Science. To arrange (strings of characters) into a chained list.
it's the dot (.) operator in php.
Posted: Sat Feb 12, 2005 12:19 am
by HuntHerr
Okay....still dont understand what to do the
Code: Select all
$res .= "".$rowї'final']."<br>";
where do i concat?
Posted: Sat Feb 12, 2005 12:22 am
by feyd
you don't do anything to that line, only the first three lines I commented.
Posted: Sat Feb 12, 2005 12:25 am
by HuntHerr
thanks