2016-08-15 47 views
-1

我在表中使用了引导模式,并且在那个模式中我想显示一个图片,它的源代码位于我正在迭代并从中获取的数组中。但图片不显示...如何在循环中提供源代码到img标签php

这是我的代码

<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) 
+1

呼应结果'<?PHP的回声的preg_replace( '/ \ S + /', '%20',$ HC [ '图像'])?>'或'<?= preg_replace函数('/ \ S + /','%20',$ hc ['image'])?> –

+0

OMG !!!我的天啊!!! OMGGGG !!!!!!!!!!!!!!!!!所有那些技能转向粉尘!当您直接编码8小时时,会发生这种情况! –

+0

足够今天编程!谢谢大家 –

回答

0

的需求preg_replace要呼应。所以,做

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

此外\s是一种白色空间,+是一个或多个前面的字符,所以此正则表达式与单个URL编码的空格代替多个空格。这可能不会按预期工作。我只是使用内置的rawurlencodeurlencode

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

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

urlencode使用+的空间,该rawurlencode将使用%20

或者用一个url编码空间交换每个空间。

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

等一下,为什么图像源不迭代?我的意思是每一行都应该有不同的形象吧? –

+0

不知道,$ $ hayatcomputers是什么样的? – chris85

+0

Array([0] => name => abc,image => adad,price => 123,[1] => name => aasdsad,image => bvbv,price => 45454) 这就是它的样子像 –