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

时间:2016-08-15 12:33:05

标签: php jquery twitter-bootstrap

我在表中使用了一个bootstrap模式,在那个模态中我想显示一个图片,它的源代码在我正在迭代并从中获取的数组中。但图片不会显示......

这是我的代码

<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 个答案:

答案 0 :(得分:0)

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

\s

此外+是一个空格,<?php echo rawurlencode($hc['image']);?> 是前一个字符中的一个或多个,因此这个正则表达式用一个URL编码空格替换多个空格。这可能不会像预期的那样奏效。我只使用内置的rawurlencodeurlencode

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

urlencode

+使用rawurlencode表示空格,%20将使用<?php echo preg_replace('/\s/', '%20',$hc['image']);?>

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

    public class MainActivity extends Activity {

    private ImageView Image;
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        Image = (ImageView) findViewById(R.id.myimage);
        Image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Image.setImageResource(R.drawable.image_1);
            }
        });
    }
}