colouring a certain word in a <TD>

  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

hey i have a database wars table (phpmyadmin) and in there will be either one of the 3 options:

win
lose
draw

What i want to be able to do is highlight the word "win" in red, lose in blue etc...
I want this to be done in css so its easily updatable as i have a text box in the admin area where i can edit my cascading stylesheet.
I tried to also do this in php, preview (snippet) below:

PHP Code: [ Select ]
 
$colarray = array(
 
    'win' => 'red',
 
    'lose' => 'draw',
 
    'draw' => 'yellow'
 
);
 
 
 
echo "<TD bgcolor=006699>";
 
echo '<span style="color:'.$colarray[$myrow['winlose']].'">'.$myrow['winlose'].'</span>';
 
echo "$myrow["winlose"]";
 
}
 
 
  1.  
  2. $colarray = array(
  3.  
  4.     'win' => 'red',
  5.  
  6.     'lose' => 'draw',
  7.  
  8.     'draw' => 'yellow'
  9.  
  10. );
  11.  
  12.  
  13.  
  14. echo "<TD bgcolor=006699>";
  15.  
  16. echo '<span style="color:'.$colarray[$myrow['winlose']].'">'.$myrow['winlose'].'</span>';
  17.  
  18. echo "$myrow["winlose"]";
  19.  
  20. }
  21.  
  22.  


but still no luck.
Any ideas, for php, or even better css?
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

PHP Code: [ Select ]
 
echo "<td bgcolor=006699 class='" . $myrow['winlose'] . "'>";
 
echo $myrow['winlose'];
 
echo "</td>";
 
 
  1.  
  2. echo "<td bgcolor=006699 class='" . $myrow['winlose'] . "'>";
  3.  
  4. echo $myrow['winlose'];
  5.  
  6. echo "</td>";
  7.  
  8.  


outputs (with a win for eg):
Code: [ Select ]
<td bgcolor=006699 class='win'>win</td>


Then use css:
Code: [ Select ]
td.win {color:#F00;}
td.lose{color:#0F0;}
td.draw{color:#00F;}
  1. td.win {color:#F00;}
  2. td.lose{color:#0F0;}
  3. td.draw{color:#00F;}


and just edit the css to change colours.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

i dont understand this bit:

PHP Code: [ Select ]
echo "<td bgcolor=006699 class='" . $myrow['winlose'] . "'>";
 
echo $myrow['winlose'];
 
echo "</td>";
  1. echo "<td bgcolor=006699 class='" . $myrow['winlose'] . "'>";
  2.  
  3. echo $myrow['winlose'];
  4.  
  5. echo "</td>";


why is the class named as being " . $myrow['winlose'] ." ???? How will it know what each colour for each word needs to be?
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

From the css class selectors.

I have made the assumptions that:

$myrow['winlose'] is either "win", "lose" or "draw"
You only want the text "win", "lose" or "draw" in the cell.

Are these assumptions wrong?
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

nah not the words, you assumed right... its the way it works that has confused me.

Because i wont be inputting the words, it will be coming from a database table that will be put in to the database via a form.

Then it has an output.... in a TD.

I want it to be done automatically....
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

well your code used:
PHP Code: [ Select ]
 
$colarray[$myrow['winlose']]
 
 
  1.  
  2. $colarray[$myrow['winlose']]
  3.  
  4.  


which would require $myrow['winlose'] to be "win", "lose" or "draw", because of the array indexes you chose for $colarray

It all depends on the values you have in the database.

Is this (roughly) what you are trying to output, or am I confused:
http://www.caffeinefuelled.net/richard- ... rtable.htm
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

yeppp thats it

let me play around with it, i let you know results as soon as it works/not works.. or whatever :oops:
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Thats what the code above will (should - not tested) output, look at the source code for that page.

basically, you output win lose or draw into the cell, and you output the same string for the class. The css will do the rest.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

oh... perfect... thanks!!!
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

im still stuck on the communication that is going on between the css and the

PHP Code: [ Select ]
 
echo "<td bgcolor=006699 class='" . $myrow['winlose'] . "'>";
 
echo $myrow['winlose'];
 
 
  1.  
  2. echo "<td bgcolor=006699 class='" . $myrow['winlose'] . "'>";
  3.  
  4. echo $myrow['winlose'];
  5.  
  6.  


as im NOT(edited) use to php, when i use a class i make it "td.win" as in your example. But the class here is referring to the echo below it... but how does the css know which class the td needs?

i know it works and i shouldnt be bothered but at occasions like this i do get a bit .... :S
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

The css doesn't care about the PHP, just the <b>outputted html</b>. In the outputted html:
Code: [ Select ]
<td bgcolor=006699 class='win'>win</td>


the td is of class 'win'. The css rule says:

Quote:
If a td element has the className "win", then make all of the text <b>inside</b> the td element red.


"inside the td" means enclosed within the < td > and </ td > tags. I'd check up on your css basics if I were you, w3schools is a good place to start.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

ohhh, thanks rtm.. yeah i been making most of this site on css, and i must admit it is a very useful piece of web programming..

I will be learning more on the css. Thank you for the resource

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 63 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.