下面是HDCMS多图字段的简单使用:
HDCMS在后台添加的多图,存到数据的时候是经过序列化过的,所以在使用的时候需要进行反序列化操作:
$moreImg = M('keshi')->where(array('aid'=>$aid,'cid'=>$cid))->find(); $moreImg['moreimg'] = unserialize($moreImg['moreimg']); $moreShowImg = $moreImg['moreimg']; $this->assign('moreShowImg',$moreShowImg);
这里循环过后的 $moreShowImg是一个数组:
array(2) { ["path"]=> array(2) { [0]=> string(44) "upload/content/2017/06/12/46121497234577.png" [1]=> string(44) "upload/content/2017/06/08/35671496908609.png" } ["alt"]=> array(2) { [0]=> string(9) "图片一" [1]=> string(9) "图片二" } } //格式化 array(2) { ["path"]=>array(2) { [0]=> string(44) "upload/content/2017/06/12/46121497234577.png" [1]=> string(44) "upload/content/2017/06/08/35671496908609.png" } ["alt"]=> array(2) { [0]=> string(9) "图片一" [1]=> string(9) "图片二" } }
获得上面图片地址,以及图片的alt属性;
方法一:HDCMS自带的标签:
<!-- 简单获取图片 --> <foreach from="$moreShowImg['path']" key="$k" value="$v"> <img src="{$v}"/> </foreach> <!-- 获取带有alt描述的图片 --> <foreach from="$moreShowImg['path']" key="$k" value="$v"> <img src="{$v}" alt="{$moreShowImg['alt'][$k]}"> </foreach>
方法二:直接通过PHP标签来做:
<!-- 简单获取图片 --> <?php foreach ($keshiShowImg['path'] as $k=>$v): ?> <img src="<?php echo $v;?>" alt="<?php echo $keshiShowImg['alt'][$k] ?>"/> <?php endforeach; ?> <!-- 获取带有alt描述的图片 --> <?php foreach ($keshiShowImg['path'] as $k=>$v): ?> <img src="<?php echo $v;?>" alt="<?php echo $keshiShowImg['alt'][$k] ?>"/> <?php endforeach; ?>
第二种方法更加灵活吧!个人觉得:(还可以嵌套其他)
<?php foreach ($keshiShowImg['path'] as $k=>$v): ?> <?php if($key==0): ?> <img src="<?php echo $v;?>" class="imgHover" alt="<?php echo $keshiShowImg['alt'][$k] ?>"/> <?php endif; ?> <?php if($key>0): ?> <img src="<?php echo $v;?>" alt="<?php echo $keshiShowImg['alt'][$k] ?>"/> <?php endif; ?> <?php endforeach; ?>