使用PHP根据SQL查询数据库中的值更改表的颜色 [英] Change color of table based on values from SQL Query database using PHP
本文介绍了使用PHP根据SQL查询数据库中的值更改表的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些代码可以从SQL查询中生成一个表。我希望单元格的背景颜色代表rel.cat的值,该值可以是1-8之间的整数。
<!DOCTYPE HTML>
< html>
< head>
< / head>
< body>
<?php
//连接数据库服务器
$ dbcnx = mysql_connect(xxxxx,xxxxx,xxxxx);
if(!$ dbcnx){
echo(< P>数据库连接失败< / P>);
exit();
}
//选择矩阵数据库数据库
if(!@mysql_select_db(sustaina_matrix)){
echo(< P>未连接到矩阵数据库& P);
exit();
}
//分配查询
$ query =选择rel.id,rel.cat colourcode FROM rel;
//执行查询
$ result = mysql_query($ query);
if(!$ result){
die(无法查询数据库:< br />。mysql_error());
}
?>
< table>
< tr>
Relationship ID< / th>
< th>颜色ID< / th>
< / tr>
<?php
//改变颜色
函数getcolour()
{
if($ catc =1)
return'#000000 ;
elseif($ catc =2)
返回'#C0C0C0';
elseif($ catc =3)
返回'#00FF00';
elseif($ catc =4)
返回'#0000FF';
elseif($ catc =5)
返回'#FF99FF';
elseif($ catc =6)
return'#FF9900';
elseif($ catc =7)
return'#FF0000';
else
返回'#FFFFFF';
//获取并显示结果
while($ row = mysql_fetch_array($ result,MYSQL_ASSOC)){
$ id = $ row [id];
$ catc = $ row [colourcode];
回显< tr>;
echo< td> $ id< / td>;
echo< td bgcolor ='getcolour()'> $ catc< / td>;
回声< / tr>;
}
?>
< / table>
< / body>
< / html>
目前所有单元都是红色的,我不知道为什么。
解决方案
您有几个问题。
code> $ catc 到函数 getcolour()
。 ==
)不是赋值( =
)。代码:
function getcolour($ catc){
// ...现有代码
}
echo< td bgcolor ='。 getcolour($ catc)。 > $ CATC< / TD>中;
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.
解决方案
You have several problems.
- Pass and accept
$catc
to the functiongetcolour()
. - 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>";
这篇关于使用PHP根据SQL查询数据库中的值更改表的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文