这是我的代码。
前2个div是空的并且乱序,如何用包含的php文件做正确的div框
<html>
<head></head>
<body>
<?php
$dir = 'folder/';
$files = scandir($dir);
$count=0;
foreach($files as $file){
$count++;
echo '<div style="float:left; margin: 0 0 10px 10px;border:1px solid #50A4AB; width:200px"><br>';
if(strpos($file,".php")){
include($dir.$file);
}
echo '</div><br>';
if($count==7){echo'<div style="clear:both;"></div><br>';}
}
?>
</body>
</html>
答案 0 :(得分:3)
scandir函数返回'。'和'..',你必须从你的结果中排除。这就是为什么前两个div是空的。
您还应该查看the documentation for the scandir function以获取有关订单的信息。
您的HTML格式错误,导致StackOverflow代码突出显示器断开,并且您的缩进不一致。在您寻求帮助时,您应该付出更多努力使您的示例易于阅读。
答案 1 :(得分:0)
if ($file != "." && $file !="..") {
// Do stuff here
}
在您的代码中,如下:
foreach($files as $file){
if ($file != "." && $file !="..") {
$count++;
echo '<div style="float:left; margin: 0 0 10px 10px;border:1px solid #50A4AB; width:200px"><br>';
if(strpos($file,".php")){
// This is a big security vulnerability
include($dir.$file);
}
echo '</div><br>';
if($count==7){echo'<div style="clear:both;"></div><br>';}
}
}