php - 根据数据库中的值在 div 中显示图像

标签 php mysql css html

我一直在做一个元素,但我遇到了困难。我有一个数据库,其中包含一些机器的工作状态。状态值从 1 到 5。我需要能够根据该 Mahcine 的数据库中显示的值在网页中为每台机器显示不同的图像。我对如何执行此操作一无所知。我使用的是 MySQL 数据库,所有内容都是用 PHP 编写的。

基本上就是这样。如果机器的状态值为 1,则它显示绿色图像。如果该值为 2,则它将是黄色,依此类推。 . .

希望大家帮忙

最佳答案

你可以尝试这样的事情:

// 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" />'
    }
}

关于php - 根据数据库中的值在 div 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11806998/

相关文章:

html - CSS,如何使滚动条在 100% 视口(viewport)中消失

php - PHP是否可以同时解析多个函数?

PHP 和 XPath 循环

php - 创建所见即所得编辑器

PHP - 将字符串递归替换为数字需要很长时间

html - 将元素设置为 2fr 而不是 1fr CSS 网格列

mysql - 如何根据 start_date 查找财政季度

mysql - 使用多个 if 语句更新 MYSQL 表

wordpress - 摆脱 WordPress MySQL 查询中的文件排序

css - W3C CSS 语法,语法怪癖