显示图像库的PHP问题
我正在尝试用PHP制作一个图库。我想从文件夹中取出所有图像,然后以3行显示它们。我有点让它工作,但前两张图片搞砸了 这就是我尝试过的:显示图像库的PHP问题,php,html,Php,Html,我正在尝试用PHP制作一个图库。我想从文件夹中取出所有图像,然后以3行显示它们。我有点让它工作,但前两张图片搞砸了 这就是我尝试过的: $images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img*.{png,jpg,gif}", GLOB_BRACE); echo '<table width="100%>'; $count="-1"; foreach($images as $image) { if ($count%3 == 1) {
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img*.{png,jpg,gif}", GLOB_BRACE);
echo '<table width="100%>';
$count="-1";
foreach($images as $image) {
if ($count%3 == 1) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery", "", $image);
echo '<td width="33%"><div class="gallery">';
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
if ($count%3 == 3) {
echo '</tr>';
}
//echo $count;
$count++;
//echo "|".$count;
}
if ($count%3 != 1) {
echo ',</tr>';
}
echo '</table>';
//echo print_r($images);
$images=glob(“$\u SERVER[DOCUMENT\u ROOT]/gallery/img*{png,jpg,gif}”,glob\u括号);
echo'我认为您的$count
初始值有问题
试试这个:
$count="3";
foreach($images as $image) {
if ($count%3 == 0) {
echo '<tr>';
}
$count++;
...
$count=“3”;
foreach($images作为$image){
如果($count%3==0){
回声';
}
$count++;
...
您的代码中有一些错误(请参阅注释)。请尝试以下操作:
$images = glob("$_SERVER[DOCUMENT_ROOT]/gallery/img/*.{png,jpg,gif}", GLOB_BRACE);
echo '<table style="width:100%">'; // error was here (missing ")
$count = 0; // error was here (counter = "-1")
foreach ($images as $image) {
// start <tr> on 0
if ($count == 0) {
echo '<tr>';
}
$url=str_replace("/home/#####/public_html/gallery/", "", $image);
echo '<td style="width:33%"><div class="gallery">'; // alternative
echo '<img onclick="window.location='.$url.'" src="'.$url.'" alt="Image Alt" width="400" height="300">';
echo '</div></td>';
// end tr at 3
if ($count == 3) {
echo '</tr>';
// reset counter
$count = -1;
}
$count++;
}
echo '</table>';
$images=glob(“$\u SERVER[DOCUMENT\u ROOT]/gallery/img/.{png,jpg,gif}”,glob\u括号);
echo“”;//此处有错误(缺少)
$count=0;//此处有错误(计数器=“-1”)
foreach($images作为$image){
//从0开始
如果($count==0){
回声';
}
$url=str_replace(“/home//home//public\u html/gallery/”,“”,$image);
回显“”;//可选
回声';
回声';
//在3点结束tr
如果($count==3){
回声';
//复位计数器
$count=-1;
}
$count++;
}
回声';
你能分享实际生成的HTML吗?这样更容易看出哪里出了问题。照片都是..而且我不能准确地检查手机上的元素..我认为最好显示代码中的错误。