1

我有一些代码可以从 SQL 查询生成表。我希望单元格的背景颜色代表“rel.cat”的值,可以是 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>

目前所有的细胞都是红色的,我不知道为什么。

4

6 回答 6

5

your if statements should have a double "==".

Instead of

if ($catc = "1")

it should be

if ($catc == "1")

Assign == everywhere in all your if conditions.

And also assign the parameter to the function.

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\")'>$catc</td>";
    echo "</tr>";
}

Get the parameter in the function as well.

function getcolour($catc)
{
于 2012-10-22T15:41:19.870 回答
2

你有几个问题。

  • 传递$catc给函数接受getcolour()
  • 在您的条件中使用逻辑运算符 ( ==) 而不是赋值 ( =)。
  • 正确输出函数结果。

代码:

function getcolour($catc) {
    // ... existing code
}

echo "<td bgcolor='" . getcolour($catc) . "'>$catc</td>";
于 2012-10-22T15:42:11.120 回答
2

Supply the variable $catc to your function. i.e.

function getcolour($catc) {
    // ... existing code
}

echo "<td bgcolor='getcolour($catc)'>$catc</td>";

You might find it more readable to use a switch() instead

function getcolour($catc) {
    switch($catc)
    {
        case 1:
            return '#000000';
            break;
        case 2:
            return '#C0C0C0';
            break;
        case 3:
            return '#00FF00';
            break;
        case 4:
            return '#0000FF';
            break;
        case 5:
            return '#FF99FF';
            break;
        case 6:
            return '#FF9900';
            break;
        case 7:
            return '#FF0000';
            break;
        default:
            return '#FFFFFF';
            break;
    }
}

echo '<td bgcolor="' . getcolour($catc) . '">' . $catc . '</td>';
于 2012-10-22T15:39:31.863 回答
1

您没有将值传递给getcolour函数。该函数也需要更改,此时 if 语句分配一个值而不是比较值,因此第一个 if 语句将始终为真。将每一项更改为“==”而不是“=”。

改变这个:

echo "<td bgcolor='getcolour()'>$catc</td>";

对此:

echo "<td bgcolor='".getcolour($row['catc'])."'>.$row['catc']</td>";

这假定 catc 从查询中返回为“catc”。

于 2012-10-22T15:39:08.663 回答
0

基于此处给出的所有 awnsers 是最终的工作代码:

<!DOCTYPE HTML>
<html>
<head>
<meta content="en-gb" http-equiv="Content-Language">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Relationships</title>
<link href="mainstyles.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
// Connect to database
require($DOCUMENT_ROOT . "connect.php");
// Assign the query
$query = "SELECT rel.id, rel.cat colourcode FROM rel";
// Execute the query
$result = mysqli_query($link ,$query);
if (!$result){
    die ("Could not query the database: <br />". mysqli_error());
}
?>
<table>
    <tr>
        <th>Relationship ID</th>
        <th>Colour ID</th>
</tr>
<?php
// Change colours
function getcolour($catc) { 
    switch($catc) 
    { 
        case 1: 
            return '#000000'; 
            break; 
        case 2: 
            return '#C0C0C0'; 
            break; 
        case 3: 
            return '#00FF00'; 
            break; 
        case 4: 
            return '#0000FF'; 
            break; 
        case 5: 
            return '#FF99FF'; 
            break; 
        case 6: 
            return '#FF9900'; 
            break; 
        case 7: 
            return '#FF0000'; 
            break; 
        default: 
            return '#FFFFFF'; 
            break; 
    } 
} 
// Fetch and display the results
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
    $id = $row["id"];
    $catc = $row["colourcode"];
    echo "<tr>";
    echo "<td>$id</td>";
    echo "<td bgcolor='" . getcolour($catc) . "'>$catc</td>";
    echo "</tr>";
}
?>
</table>
</body>
</html>
于 2012-10-23T10:47:37.643 回答
0
 function getcolour($catc){
..........etc

并称之为

<td bgcolor='",getcolour($catc),"'>$catc</td>

并且不要忘记不推荐使用 bgcolor =)

于 2012-10-22T15:46:17.880 回答