Home > Backend Development > PHP Tutorial > 怎么将数据库里面内容以不同的颜色显示在页面下

怎么将数据库里面内容以不同的颜色显示在页面下

WBOY
Release: 2016-06-13 12:53:00
Original
1127 people have browsed it

如何将数据库里面内容以不同的颜色显示在页面上?
如题。比如在数据库database1下面有张表叫table1,表有七列,每列的值都为0、1或2.如何将此张表显示在网面上,且要求当数值为0时显示红色;当数值为1时显示黄色;当数值为2时显示为绿色???


------解决方案--------------------
通过设置样式表
------解决方案--------------------
<br />
$style = array(0=>"red",1=>"yellow",2=>"gree");<br />
根据数据库的值 输出样式<br />
Copy after login



------解决方案--------------------
会用PHP输出表格吗?类似
<br />
	$ar1=array(0,1,2);<br />
	$ar2=array(0,1,2);<br />
	$ar3=array(0,1,2);<br />
	$ar4=array(0,1,2);<br />
	$ar5=array(0,1,2);<br />
	$ar6=array(0,1,2);<br />
	$ar7=array(0,1,2);<br />
	<br />
<br />
<table border="1" cellspacing="0" cellpadding="0" width="600"><br />
	for($i=0;$i<count($ar1);$i++){<br />
		echo "<tr>";<br />
		echo "<td>".$ar1[$i]."</td>";<br />
		echo "<td>".$ar2[$i]."</td>";<br />
		echo "</tr>";<br />
	}<br />
</table><br />
<br />
Copy after login

假设输出后的表格是下面的形式
<br />
<style type="text/css"><br />
.r{<br />
background-color:red;<br />
}<br />
.y{<br />
background-color:yellow;<br />
}<br />
.g{<br />
background-color:green;<br />
}<br />
</style><br />
<script src="http://code.jquery.com/jquery-latest.js"></script><br />
<script type="text/javascript"><br />
	$(function(){<br />
		$("#abv").click(function(){<br />
			$("tr td").each(function(){<br />
				if($(this).text()==0){<br />
					$(this).addClass("r");<br />
				}else if($(this).text()==1){<br />
					$(this).addClass("y");<br />
				}else{<br />
					$(this).addClass("g");<br />
				}<br />
			});<br />
		});<br />
	})<br />
 </script><br />
<input type="button" id="abv" value="测试用按钮"/><br />
<table border="1" cellspacing="0" cellpadding="0" width="600"><br />
 <tr><br />
	<td/>0</td><br />
	<td>2</td><br />
	<td>2</td><br />
	<td>1</td><br />
	<td>2</td><br />
	<td>2</td><br />
	<td>1</td><br />
 </tr><br />
 <tr><br />
	<td>2</td><br />
	<td>2</td><br />
	<td>1</td><br />
	<td/>1</td><br />
	<td>1</td><br />
	<td>2</td><br />
	<td>0</td><br />
 </tr><br />
 <tr><br />
	<td/>0</td><br />
	<td>1</td><br />
	<td>2</td><br />
	<td/>0</td><br />
	<td>1</td><br />
	<td>2</td><br />
	<td>0</td><br />
 </tr><br />
</table><br />
<br />
Copy after login

------解决方案--------------------
<style type="text/css"><br />
.col_0 {<br />
background-color:red;<br />
}<br />
.col_1 {<br />
background-color:yellow;<br />
}<br />
.col_2 {<br />
background-color:green;<br />
}<br />
</style>
Copy after login

while($row=mysql_fetch_array($result))<br />
{<br />
  echo '<tr>';<br />
  for($i=0; $i<7; $i++) {<br />
    echo "<td class='col_{$row[$i]}'>{$row[$i]}</td>";<br />
  }<br />
  echo '</tr>';<br />
}
Copy after login

------解决方案--------------------
引用:
引用:PHP code?12$style = array(0=>"red",1=>"yellow",2=>"gree");根据数据库的值 输出样式

谢谢,能不能说是具体一点。就是说$style = array(0=>"red",1=>"yellow",2=>"gree");这句话应该放到下面代码的哪里呢?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template