我一直在做一个项目,但我已经到了我被卡住的地步。我有一个数据库,其中包含一些mahcines的工作状态。状态值从1-5开始。我需要能够根据该Mahcine的数据库中显示的值为网页中的每台机器显示不同的图像。我在如何做到这一点上画了一大笔空白。我使用MySQL数据库,一切都是用PHP编写的。
基本上就是这个。如果机器的状态值为1,则显示绿色图像。如果值为2,则它将为黄色,依此类推。 。
希望你们能帮忙
答案 0 :(得分:0)
您可以尝试这样的事情:
// your mysql select, wich contains the machine data.
$query = mysql_query("select the data about machines...");
// you iterate on the result set and fetch each row to $data
while($data = mysql_fetch_array($query))
{
switch($data['machine'])
{
case "machine type 1": // you can put integer values here as well, like case 1:
echo '<img src="first_machine.jpg" alt = "first machine" />'
break;
case "machine type 2":
echo '<img src="second_machine.jpg" alt = "second machine" />'
break;
default: // undefinied
echo '<img src = "undefinied.jpg" alt = "undefinied" />'
}
}
答案 1 :(得分:0)
请勿使用img标记,而是创建一个div,您可以为其应用与机器状态值相同的样式类
<div class="machine status<?php echo $status;?>" ></div>
现在在你的CSS中,
.status1{
background-image:url(red.jpg);
}
.status2{
background-image:url(green.jpg);
}
.status3{
background-image:url(jpg.jpg);
}
.machine{
width:50px;
height:50px;
}
答案 2 :(得分:0)
Ok you can't display multiple images within a image/jpeg page...
You're telling the browser that the page is image/jpeg (in other words, the page is AN IMAGE) but you're echoing out multiple image data
You should rather use the gallery page to show all images like this:
<?php
// $images = result from database of all image rows
foreach ($images as $img) echo '<img src="img.php?id='.$img["id"].'">';
?>
and in img.php:
// Load the image data for id in $_GET['id'];
header("Content-type: image/jpeg");
echo $data;