我以$product_image='img1.png,img2.png,img3.png'
作为字符串,在其中爆炸$product_image
并将图像显示为$pro_img[0], $pro_img[1] and $pro_img[2]
。
我收到错误
消息:未定义的偏移量:2
如果$pro_img[2]
未找到任何图像。
我想使用foreach loop
来解决它,但是我不知道如何使用foreach
来解决这个问题?
<?php $pro_img = explode(",",$product_image); ?>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="pro-large text-center">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 35%;" alt="">
</div>
</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<div class="pro-large text-center">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 35%;" alt="">
</div>
</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
<div class="pro-large text-center">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 35%;" alt="">
</div>
</div>
</div>
<ul class="nav product-img-tab" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 96px;" alt="">
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 96px;" alt="">
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 96px;" alt="">
</a>
</li>
</ul>
答案 0 :(得分:-1)
由于您有不同的部分(家庭/个人资料/联系人),因此我假设您始终要显示带或不带图像,因此建议改用isset
。
Isset只是检查$ pro_img数组中是否存在键,您可以使用它来决定是否打印图像标签:
<?php
$pro_img = explode(",",$product_image);
?>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="pro-large text-center">
<?php if (isset($pro_img[0])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 35%;" alt="">
<?php endif; ?>
</div>
</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<div class="pro-large text-center">
<?php if (isset($pro_img[1])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 35%;" alt="">
<?php endif; ?>
</div>
</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
<div class="pro-large text-center">
<?php if (isset($pro_img[2])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 35%;" alt="">
<?php endif; ?>
</div>
</div>
</div>
<ul class="nav product-img-tab" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">
<?php if (isset($pro_img[0])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 96px;" alt="">
<?php endif; ?>
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">
<?php if (isset($pro_img[1])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 96px;" alt="">
<?php endif; ?>
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">
<?php if (isset($pro_img[2])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 96px;" alt="">
<?php endif; ?>
</a>
</li>
</ul>