Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6705745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:26:22+00:00 2026-05-26T07:26:22+00:00

I’m working on a corporate website in which you retrieve (via a MySQL consult)

  • 0

I’m working on a corporate website in which you retrieve (via a MySQL consult) some data that are formatted on a HTML table. The problem comes when trying to change the row color if a specific field value is one or another.

The (partial) code is:

    $RowCt = 0;
while($Row = mysql_fetch_assoc($Result))
{   
    $timezone = new DateTimeZone( "Europe/London" );
    $date = new DateTime();
    $date->setTimezone( $timezone );
    $ahora = $date->format( 'H:i' ); 

    if ($var == 1) {
        $rowcolor = "red";
    } else if ($var == 0) {
        $rowcolor = "yellow";
    } else {
        $rowcolor = "white";
    }

    //$Table.= "<tr style='background-color:#FFFFFF;'>";

    foreach($Row as $field => $value)
    {                   
        switch ($field) {
            default:
                $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>$value</td>"; 
                break;  
            case a:          
                if ($now > $value) {
                        $var == 0;
                    } else {    
                        // Do nothing
                    }
                $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>$value</td>"; 
                break;
            case b:
                if ($now > $value) {
                        $var == 1;
                    } else {    
                        // Do nothing
                    }
                $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>$value</td>"; 
                break;
            case c:
                switch ($value) {
                    case 1:
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>a</td>";
                        break;
                    case 2:
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>b</td>";
                        break;
                }
            case d:
                switch ($value) {
                    case 3:
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>a</td>";
                        break;
                    case 4:
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>b</td>";
                        break;
                    case 5:
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>c</td>";
                        break;
                    case 6:
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>d</td>";
                        break;
                }
            case e:
                switch ($value) {
                    case "E":
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>a</td>";
                        break;
                    case "I":
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>b</td>";
                        break;
                    case "F":
                        $Table.= "<td style='font-size:14px; background-color:$rowcolor' align='center'>c</td>";
                        break;
                }
        }
    }
$Table.= "</tr>";

The problem comes when trying to define a value inside of the switch statement, and then trying to make it be a condition so the entire row is colored (yellow if case 1 is selected and we aren’t on time; red if case 2 is selected and we aren’t on time).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:26:23+00:00Added an answer on May 26, 2026 at 7:26 am

    Your code snippet doesnt really explain much, but from i can see, i would advise your switch differently. Your switch is building HTML output.

    I would encourage using switch to rather set the style in a $style variable, also… strictly speaking it makes better logic to use case: first and then default: eg.

    switch ($condition) {
      case 1 : $style = 'background:red;'; break;
      case 2 : $style = 'background:yellow;'; break;
      default : $style = 'background:white;';
    }
    

    now build your HTML eg.

    foreach ($results as $row) echo '<tr><td style="'.$style.'">Cell data</td></tr>';
    

    For multiple cases on switch:

    switch ($condition) {
      case 0:
      case 1:
        // Do something
        break;
      case 2: /* Do something */ break;
      default: /* Do something */ break;
    }
    

    2 Tier switch:

    switch ($condition_1) {
      case 0 :
        switch ($condition_2) {
          case 'A' :
          // do something
          break;
        }
      break;
    }
    

    Don’t forget to BREAK in both levels

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.