如何在php中提供img标签循环的源代码



我在一个表中使用了一个引导模态,在这个模态中,我想显示一张图片,它的源是在一个数组中,我正在迭代并从中得到。但是图片显示不出来…

这是我的代码

<table class='table table-hover table-responsive' width='21' id='tableHayatcomputers'>
<caption>Hayat Computers</caption>
<?php foreach($hayatcomputers as $hc){ ?>
<tr>
<td><?php echo $hc['name'] ?></td>
<td><?php echo $hc['price'] ?></td>
<td><div class="container">
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal">Open Modal</button>
  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
    Details
      <button type="button" class="close" data-dismiss="modal">&times;</button>
    </div>
    <div class="modal-body">
      <img src="<?php preg_replace('/s+/', '%20',$hc['image'])?>" class="img-thumbnail" alt="Cinque Terre" width="304" height="236"></img>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
    </div>
  </div>
</div>
</td>
</tr>
<?php } ?>
</table>

我遗漏了什么参考吗?为什么图像不迭代,而名称和价格是

array (size=2)
  0 => 
array (size=4)
  'name' => string ' Nvidia Geforce STRIX-GTX970-DC2OC-4GD5 GTX 970' (length=47)
  'price' => string '
                                    Rs. 43,800                                                              Ex Tax: Rs. 43,800
                                ' (length=64)
  'link' => string 'http://www.hayatcomputers.com/index.php?route=product/product&product_id=1551&search=GTX+970' (length=92)
  'image' => string 'http://www.hayatcomputers.com/image/cache/data/Nvidia Geforce/11-200x236.jpg' (length=76)
  1 => 
array (size=4)
  'name' => string 'NVIDIA GeForce GTX 970' (length=22)
  'price' => string '
                                    Rs. 43,000                                                          Ex Tax: Rs. 43,000
                                ' (length=64)
  'link' => string 'http://www.hayatcomputers.com/index.php?route=product/product&product_id=794&search=GTX+970' (length=91)
  'image' => string 'http://www.hayatcomputers.com/image/cache/data/Graphics Cards/NVIDIA GeForce GTX 970  Rev 1.1-200x236.jpg' (length=105)

需要回显preg_replace

<?php echo preg_replace('/s+/', '%20',$hc['image']);?>

另外s是一个空格,+是前面的一个或多个字符,所以这个正则表达式用一个URL编码的空格替换了多个空格。这可能不会像预期的那样起作用。我只使用内置的rawurlencode或urlencode。

<?php echo rawurlencode($hc['image']);?>

<?php echo urlencode($hc['image']);?>

urlencode使用+作为空格,rawurlencode将使用%20

或者将每个空格替换为一个url编码的空格。

<?php echo preg_replace('/s/', '%20',$hc['image']);?>

最新更新