无法在聊天应用中为管理员和简单用户显示特定图片
问题描述:
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!
如果是管理员或简单用户,请尝试为登录用户显示不同的图片。 这是我的函数 p>
function isAdminPhotoChange(){
//检查用户是admin还是user
if(isset($ _ SESSION ['user'])&amp; &amp; $ _SESSION ['user'] ['user_type'] =='admin'){
if(file_exists(“images / metcircle13.png”)){
$ filename =“$ metcircle13.png”; \ n echo'&lt; img src =“images /&lt;?php echo'。$ filename。'?&gt;” style =“height:50px;”&gt;';
} else {
if(isset($ _ SESSION ['user'])&amp;&amp; $ _SESSION ['user'] ['user_type'] ==' user'){
$ filename =“user_profile.jpg”;
echo'&lt; img src =“images /&lt;?php echo'。$ filename。'?&gt;” style =“height:50px;”&gt;';
}
}
}
}
code> pre>
这是另一个php文件中的调用 p>
&lt;?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'];
}
?&gt;
&lt; div class =“sxolion”&gt;
&lt; strong style =“margin:3px; color:#000; text-shadow:2px 2px 5px #3d5c5c;“&gt;&lt;?php isAdminPhotoChange(); ?&gt;&lt; br&gt;&lt; p style =“margin:4px;”&gt;&lt;?= $ name?&gt;&lt; / p&gt;&lt; / strong&gt;&lt; p style =“margin:3px;” &gt;&lt;?= $ comment?&gt;&lt; / p&gt;&lt; span class =“time”&gt;&lt; br&gt;&lt; p style =“margin:4px;”&gt;&lt;?= date(“ j / m / Y g:i:sa“,strtotime($ time))?&gt;&lt; / p&gt;&lt; / span&gt;
code> pre>
p>
谢谢! p>
div>
答
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;">';
}
}