doubi4617 2017-05-21 23:58
浏览 21

为什么这个php变量不会进入html的id?

I'm a highschool student who is not that great at coding, so forgive me if I sound dumb for this or have missed something obvious. So I have div ids that play audio clips when I click on pictures. They go from div5 to div14. When I type in 'div5' directly after 'id =' then it plays my audio. However, I'm trying to make this id change in increments of one (div5,div6,div7 etc). So I tried that loop. I found out that the html wont take the php variable $string, as when I tried it, the picture on my website didn't play audio. The $i doesn't do anything atm. If anyone could help, I would greatly appreciate it.

<?php
$i=5;
while ($row= mysqli_fetch_assoc($result)) {
$string="drag5";

echo '<div style= "width: 100px;  height: 100px;  margin: 10px;border: 3px 
solid #FFFFFF; box-shadow: 10px 10px 5px #888888; float: left" 
class="boxes"><img height="100px" width="100px" id="<?php echo $string; ?>" 
class="dragg" src="data:image/jpeg;base64,'.base64_encode( $row['source'] 
).'"/></div>';
$i++;
}
?>
  • 写回答

1条回答 默认 最新

  • doudiecai1572 2017-05-22 00:11
    关注

    There are a bunch of issues with your sample code, which is probably why you're having a hard time finding the immediate culprit. Here's an approach that, while still pretty far from perfect, might be a bit clearer:

    <style>
        .boxes {
            width: 100px;
            height: 100px;
            margin: 10px;
            border: 3px solid #FFFFFF;
            box-shadow: 10px 10px 5px #888888;
            float: left;
        }
        .boxes > img {
            height: 100px;
            width: 100px;
        }
    </style>
    <?php
    
    $i = 5;
    
    while ($row= mysqli_fetch_assoc($result)) {
        printf('
            <div class="boxes">
                <img id="string%s" class="dragg" src="data:image/jpeg;base64,%s"/>
            </div>',
            $i,
            base64_encode($row['source'])
        );
        $i++;
    }
    ?>
    

    I pulled all the styling out of the php (you should do the same, but should probably put it into a separate stylesheet). I also opted for printf rather than echo, but you could switch to a double-quoted string and use variable interpolation instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大