douguai4653 2018-11-02 06:26
浏览 46
已采纳

无法在聊天应用中为管理员和简单用户显示特定图片

Trying to display a different pic for the logged in user if it's an admin or a simple user. Here is my function

function isAdminPhotoChange() {
    // check if user is admin or user
    if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin') {
        if (file_exists("images/metcircle13.png")) {
            $filename = "$metcircle13.png";
            echo '<img src="images/<?php echo'. $filename.'?>" style="height: 50px;">';
        } else {
            if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'user') {
                $filename = "user_profile.jpg";
                echo '<img src="images/<?php echo'. $filename.'?>" style="height: 50px;">';
            }
        }
    }
}

And here is the call in another php file

<?php
include("functions.php");

$comm = mysqli_query($db, "select name,comment,post_time from comments");
while($row=mysqli_fetch_array($comm)){

    $name=$row['name'];
    $comment=$row['comment'];
    $time=$row['post_time'];
}

?>
<div class="sxolion">
<strong style="margin: 3px; color: #000; text-shadow: 2px 2px 5px #3d5c5c;"><?php isAdminPhotoChange(); ?><br><p style="margin: 4px;"><?=$name?></p></strong><p style="margin: 3px;"><?=$comment?></p><span class="time"><br><p style="margin: 4px;"><?=date("j/m/Y g:i:sa", strtotime($time))?></p></span>

Thank You!

展开全部

  • 写回答

1条回答 默认 最新

  • douwo1517 2018-11-02 06:45
    关注

    In the nicest way possible, there was an awful lot of logic/syntax errors in your isAdminPhotoChange() function that have been mentioned in the comments, and some more - though none that would of thrown an error.

    Here's my attempt at fixing just that function, as the rest appears OK:

    function isAdminPhotoChange()
    {
        // ENSURE SESSION HAS STARTED
        if(session_status() === PHP_SESSION_NONE)
        {
            session_start();
        }
    
        $admin_img = "images/metcircle13.png";
        $user_img = "images/user_profile.jpg";
    
        // IS ADMIN
        if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'admin' && file_exists($admin_img))
        {
            echo '<img src="'.$admin_img.'" style="height: 50px;">';
        }
        // IS USER
        else if (isset($_SESSION['user']) && $_SESSION['user']['user_type'] == 'user' && file_exists($user_img))
        {
            echo '<img src="'.$user_img.'" style="height: 50px;">';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部