dqssst0144 2017-02-13 15:33
浏览 32
已采纳

foreach循环内链接的每个图像的photo_id

What I'm trying to do is get the photo_id='.$pid.' of each photo for the link wrapped around them inside the foreach loop.

I tried $image['photo_id'] but that didn't work it just printed image symbols. I then wrapped a separate foreach loop around the current one and that duplicated the amount of images. I'm new to using the foreach loop with arrays and keys so still trying to get my head around it.

$images = array();
while ($rowhhh = mysqli_fetch_assoc($resulthhh)) {
  $images[] = $rowhhh['photo_imagedata'];
  $photo_id[] = $rowhhh['photo_id'];
}

foreach ($images as  $image) {
if($photo_num==1){
echo '<a href="photo.php?photo_id='.$pid.'"><img class="stream_images" style="width:200px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image) .'" /></a>';
}else{
echo '<a href="photo.php?photo_id='.$pid.'"><img class="stream_images" style="width:100px;height:100px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image) .'" /></a>';
}
}
  • 写回答

1条回答 默认 最新

  • douyong1886 2017-02-13 15:49
    关注

    Personally, I'd use the $images var as a multidimensional array to store the ID and the Data of each image. Then, you can access both of these bits of information easily in your foreach() loop.

    $images = array();
    while ($rowhhh = mysqli_fetch_assoc($resulthhh)) {
      $imageArray = array(
           'data' => $rowhhh['photo_imagedata'],
           'id' => $rowhhh['photo_id']
      );
    
      $images[] = $imageArray;
    }
    
    foreach ($images as  $image) {
        if($photo_num==1){
            echo '<a href="photo.php?photo_id='.$image['id'].'"><img class="stream_images" style="width:200px;height:200px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
        }else{
            echo '<a href="photo.php?photo_id='.$image['id'].'"><img class="stream_images" style="width:100px;height:100px;object-fit:cover;margin:2px;padding:2px;" src="data:image/jpeg;base64,'. base64_encode($image['data']) .'" /></a>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?