PHP:在一行中添加多个图像
问题描述:
我使用此代码获取一个文件夹中的所有图像到我的页面,但不知道如何指定每行图像的数量,例如我只想要6个图像此行 请帮助PHP:在一行中添加多个图像
<p align="center">
<?php
$dirname = "images2/boys/fig/";
$images = glob($dirname."*");
foreach($images as $image) {
echo '<img src="'.$image.'" class="a" onclick="window.open(this.src);"/>';
}
?>
答
有一对夫妇的方式来实现你想要的东西,是一个:
使用css
以及所需的DIV设置display:inline-block
,即:
div.myClass{
display:inline-block;
}
<div class="myClass">
<img src="http://placehold.it/350x150" height="100" width="100" />
</div>
<div class="myClass">
<img src="http://placehold.it/350x150" height="100" width="100" />
</div>
<div class="myClass">
<img src="http://placehold.it/350x150" height="100" width="100" />
</div>
<div class="myClass">
<img src="http://placehold.it/350x150" height="100" width="100" />
</div>
<div class="myClass">
<img src="http://placehold.it/350x150" height="100" width="100" />
</div>
<div class="myClass">
<img src="http://placehold.it/350x150" height="100" width="100" />
</div>
使用PHP
:
foreach($images as $image) {
echo <<< EOF
<div class="container">
<img src="$image" onclick="window.open(this.src);" class="myClass" height="100" width="100" />
</div>
EOF;
}
答
你必须使用CSS样式。先用一个固定的宽度和样式插入父DIV图像的图像按照你的愿望,以显示:
<div class="img_wrap">
<?php
$dirname = "images2/boys/fig/";
$images = glob($dirname."*");
foreach($images as $image) {
echo '<img src="'.$image.'" class="a" onclick="window.open(this.src);"/>';
}
?>
</div>
CSS样式:
.img_wrap {
width:600px; /*this can be any value*/
height:auto;
}
.a {
width:85px; /*this can be any value*/
height:85px; /*for getting a squared image. *this can be any value*/
display:inline;
float:left;
margin:5px;
}
感谢你们真的很感激 – zaidfs
不客气zaidf 。如果我的回答对您有帮助,请将其标记为正确答案并给予1up,谢谢! –