2017-08-05 90 views
1

我想用PHP创建一个图库。我想从一个文件夹中获取所有图像,然后将它们显示为3行。我有它的工作,但前2个图像搞砸了。显示图片库的PHP问题

这是我已经试过:

$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); 

这个工程样的,但它使这样的:

(这些只是照片,真实的照片都有点反感.. )

我知道我做错了什么,但我不知道是什么!

+1

你可以分享生成的实际HTML。使它更容易看到发生了什么问题。 – Cagy79

+0

这些照片都是..我无法准确检查手机上的元素。 –

回答

1

有在你的代码的一些错误(见注释)。也许试试这个:

$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>'; 
+1

我认为最好是在代码中显示哪些错误。 – SaidbakR

0

我认为你的$count初始值有问题。

试试这个:

$count="3"; 
foreach($images as $image) { 
    if ($count%3 == 0) { 
     echo '<tr>'; 
    } 
    $count++; 

    ...