douguai4653 2018-11-02 14: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 14: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;">';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化