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 8728425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:38:15+00:00 2026-06-13T08:38:15+00:00

I have some code to produce a table from an SQL query. I would

  • 0

I have some code to produce a table from an SQL query. I would like the background color of the cell to represent the value of “rel.cat”, which can be an integer between 1-8.

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// Connect to the database server
$dbcnx = mysql_connect("xxxxx",xxxxx,xxxxx);
if (!$dbcnx) {
  echo( "<P>Database Connection Failed</P>" );
  exit();
}
// Select the matrix databse database
  if ( !@mysql_select_db("sustaina_matrix") ) {
    echo( "<P>Not Connected to Matrix Database</P>" );
    exit();
  }
// Assign the query
$query = "SELECT rel.id, rel.cat colourcode FROM rel";
// Execute the query
$result = mysql_query($query);
if (!$result){
    die ("Could not query the database: <br />". mysql_error());
}
?>
<table>
    <tr>
        <th>Relationship ID</th>
        <th>Colour ID</th>
</tr>
<?php
// Change colours
function getcolour()
{
    if ($catc = "1")
        return '#000000';
    elseif($catc = "2")
        return '#C0C0C0';
    elseif($catc = "3")
        return '#00FF00';
    elseif($catc = "4")
        return '#0000FF';
    elseif($catc = "5")
        return '#FF99FF';
    elseif($catc = "6")
        return '#FF9900';
    elseif($catc = "7")
        return '#FF0000';
    else
        return '#FFFFFF';
}
// Fetch and display the results
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $id = $row["id"];
    $catc = $row["colourcode"];
    echo "<tr>";
    echo "<td>$id</td>";
    echo "<td bgcolor='getcolour()'>$catc</td>";
    echo "</tr>";
}
?>
</table>
</body>
</html>

Currently all the cells are red, and I don’t know why.

  • 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-06-13T08:38:17+00:00Added an answer on June 13, 2026 at 8:38 am

    You have several problems.

    • Pass and accept $catc to the function getcolour().
    • Use logical operator (==) not assignment (=) in your conditions.
    • Properly output the function results.

    Code:

    function getcolour($catc) {
        // ... existing code
    }
    
    echo "<td bgcolor='" . getcolour($catc) . "'>$catc</td>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that will change the background color of a specific label
I have to output some products from table 'products', along with the lowest price
I've written PL/SQL code to denormalize a table into a much-easer-to-query form. The code
I have some code that builds SQL queries to perform a lenient, catch-all kind
I have some .NET code that checks for the existence of a SQL record
I have some code that produces undefined values in an array making no sense.
I have some Javascript code that creates 2 arrays: One for Product Category and
I have some basic PHP code: $raceramps56[short] = My Test Product; $leftMenu = '<div
I have some code on two systems running kernel 2.4.20 and kernel 2.4.38 .
I have some code here that uses bitsets to store many 1 bit values

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.