dongliang_bj2016 2013-08-12 17:59
浏览 27
已采纳

根据变量中的值显示不同的图像

just want to make sure I'm going in the right direction with this. I have an image which I want to be replaced/changed if the value of a variable is either 0/1. So here is the code from the guy doing the server side stuff.

<?php
//Requires mysql_connect to create the connection

$link_state = 0;

//If you so wish you don't have to check for a connection, but may be a good idea leave this in.
if ($mysql_connection['connected'] == true) {
    $result = mysql_query("SELECT * FROM link");
    //The bit we are looking for should be the first row, and we should only get one row
    $count  = mysql_num_rows($result);
    if ($count <= 0) {
        //Interesting...
        $mysql_error['error']       = true;
        $mysql_error['description'] = "ERROR: No rows were returned from table 'link'";
    } else {
        //We should be ok to continue
        if ($count > 1) {
            $mysql_error['error']       = true;
            $mysql_error['description'] = "WARNING: Found more than one row in 'link' table!";
        }
        $row        = mysql_fetch_array($result);
        $link_state = intval($row['state']);
    }
} else {
    $mysql_error['error']       = true;
    $mysql_error['description'] = "ERROR: No mysql connection!";
}

/*
After the completion of this page, $link_state will be one of two things:

* 0 = offline
* 1 = online

Throws to $mysql_error:

1 Warning
2 Errors

*/
?>

Okay, so I'm assuming by that little bit of code I will then have a value of either 0 or 1 in $link_state.

So from this can I then just do a simple inline script like this to get my relevant image?

<img src="img/<?=($link_state=="0"?"off.jpg":($link_state=="1"?"on.jpg":))?>" />

Any insight would be great :)

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongtaochan0777 2013-08-12 18:03
    关注

    try this

    <?php $img = ($link_state == "0") ? "off.jpg" : "on.jpg"; ?>
    
    <img src="./img/<?php echo $img; ?>" />
    

    also use mysqli_* since mysql_* is depreciated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办