why this switch statement does not work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HuntHerr
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2003 10:17 pm

why this switch statement does not work

Post 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; 
&#125; 
$res .= "".$row&#1111;'final']."<br>"; 
&#125; 
echo $res;
&#125;
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your echo'ing the colors, yet concatenating the value you are coloring?
HuntHerr
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2003 10:17 pm

Post by HuntHerr »

Code: Select all

<?php recentmatchresult(); ?>
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>";

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you read and understand my post? .. I said what the problem is..
HuntHerr
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2003 10:17 pm

Post by HuntHerr »

did not understand it, sorry =\
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. here's your originally posted code, which I added some comments to:

Code: Select all

function recentmatchresult()
&#123;
$query = "SELECT final FROM matches ORDER BY id DESC LIMIT 0, 5";
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
&#123;
switch ($row&#1111;'final'])
&#123;
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;
&#125;
$res .= "".$row&#1111;'final']."<br>";                  // <-- concat line
&#125;
echo $res;
&#125;
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.
HuntHerr
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2003 10:17 pm

Post by HuntHerr »

Okay, so do away with the echo? I am a little confused and its late =\
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I believe you need to concat the font color with $res..
HuntHerr
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2003 10:17 pm

Post by HuntHerr »

Sorry, am not used to this term, concat
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
HuntHerr
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2003 10:17 pm

Post by HuntHerr »

Okay....still dont understand what to do the

Code: Select all

$res .= "".$row&#1111;'final']."<br>";
where do i concat?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't do anything to that line, only the first three lines I commented.

Code: Select all

$res .= '<font....';
HuntHerr
Forum Newbie
Posts: 10
Joined: Mon Jun 30, 2003 10:17 pm

Post by HuntHerr »

thanks
Post Reply