duanqie8549
2019-08-09 13:44
浏览 38
已采纳

如何使用PHP函数连接HTML <img>?

Been having trouble displaying an avatar for my website. I store all of them in a custom folder called images, where either I or the users create them. The avatar shows only if youre the exact id the avatar is named after. Example:

I have a user who's id is 1, I also have an image with a name 1.png, so this PHP code block gets the link where the images are stored at, fetches the users id and then searches the page for that image that is name after the id.

Here's the code block I've been having issues with:

<img id='avatar' 
     class='Avatar'
     src='https:/mysitelink.com/images/' + <?php echo"" . $fetchuser->id . ""; ?>.png?r=<?php echo "$RefreshRate"; ?>' 
     height='350'
     width='350'
     onerror="this.src='mysitelink.com/images/default.png'" />

The mysitelink.com is inserted by me to hide the link of the website.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqq22391 2019-08-09 14:27
    已采纳

    Try like below :

    <img id='avatar' 
         class='Avatar'
         src='https://mysitelink.com/images/<?php echo $fetchuser->id.'.png'; ?>?r=<?php echo "$RefreshRate"; ?>' 
         height='350'
         width='350'
         onerror="this.src='mysitelink.com/images/default.png'" />
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duancan7914 2019-08-09 13:52

    instead of src='https:/mysitelink.com/images/' + <?php echo . $fetchuser->id . ""; ?>.png?r=<?php echo "$RefreshRate"; ?>'

    try

      echo "src='https:/mysitelink.com/images/" . $fetchuser->id . ".png?r=" .$RefreshRate ."'>"; 
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题