Jump to content

[SOLVED] Change background COLOR for a TABLE ROW


AndieB

Recommended Posts

Hi all!

 

This has most likely been asked tons of times before, but when I did a search, so much more appear, so it felt easier to write a NEW topic.

 

I'm letting my PHP script echo out a TABLE with rows and cells, reading rows from a database. Now, if I want the background color to change in a very effective PHP script way, during the while syntax that draws up the rows in the table, how do I manage this in a very code effective way?

 

Let's say that $bgcolor = "#445566";

and it should switch to value "#667788" on every other row, then back again... it would look something like this:

 

echo "<tr style=\"background-color: " . $bgcolor . ";\" >text text text</tr >";

 

I've seen a very long time back ago, a solution in just one row of code... don't know if :: were used or something like that.

 

Anyway... anyone has some ideas?

 

Thank you all in advance!

 

--Andreas

Link to comment
Share on other sites

One way of doing this:

<?php
$bgcolor = "#445566";
for ($i=0;$i<15;$i++) { //or your loop control here
   $bgcolor = ($bgcolor == "#445566")?"#667788":"#445566";
   echo "<tr style=\"background-color: " . $bgcolor . ";\" >text text text</tr >";
} ?>

 

Ken

Link to comment
Share on other sites

One way of doing this:

<?php
$bgcolor = "#445566";
for ($i=0;$i<15;$i++) { //or your loop control here
   $bgcolor = ($bgcolor == "#445566")?"#667788":"#445566";
   echo "<tr style=\"background-color: " . $bgcolor . ";\" >text text text</tr >";
} ?>

 

Ken

 

Hi Ken!!

 

Thank you very much!

Now I recognize the row that I was talking about:

$bgcolor = ($bgcolor == "#445566")?"#667788":"#445566";

 

What is this called when you use a ? character and a : ??

It somehow switches the values, right?

 

Again, thank you very much!

 

Sincerely,

 

Andreas

Link to comment
Share on other sites

 

$bgcolor = ($bgcolor == "#445566")?"#667788":"#445566";

What is this called when you use a ? character and a : ??

It somehow switches the values, right?

 

 

You are correct it switches the value. It is a short way to notate a if else. Using this syntax is called shorthand.

 

this:

$bgcolor = ($bgcolor == "#445566")?"#667788":"#445566";

 

equals:


if($bgcolor == "#445566"){
    $bgcolor="#667788";
}else{
    $bgcolor="#445566";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.