我正在尝试打印出 php 目录中的文件,并尝试将一个列表中的文本文件和另一个列表中的 jpg 文件对齐(在相同的水平高度,并排)。
<!DOCTYPE html>
<html>
<body>
<?php
$files = scandir('uploads');
foreach($files as $file){
$extension = pathinfo($file,PATHINFO_EXTENSION);
if($extension == 'txt') {
echo'<div style="text-align:center; margin-left:-120px;"> <br><a href="uploads/'.$file.'">'.$file.'</a></div>';
}
if($extension == 'jpg') {
echo'<div style="text-align:center; margin-right:-120px;"> <br><a href="uploads/'.$file.'">'.$file.'</a></div>';
}
}
?>
</body>
</html>
我的目录中有以下文件:
test.txt hello.txt test.jpg hello.jpg
这给了我输出:(在屏幕中间
test.jpg
hello.jpg
test.txt
hello.txt
但我想要这样的东西(在页面中间),其中 .txt 文件排在第一位。
test.txt test.jpg
hello.txt hello.jpg
我试过添加 `float:left;' 的 css 元素然后将该类添加到 div 中,但这并不能解决问题。
我也尝试过使用:display: inline-block;
无济于事
最佳答案
给你
div {
text-align: center;
box-sizing: border-box;
border: 1px solid black;
width: 50%;
float:left;
}
<div>
<ul>
<li><a href="files/hello.txt">Hello.txt</a></li>
<li><a href="files/test.txt">Test.txt</a></li>
</ul>
</div>
<div>
<ul>
<li><a href="files/hello.txt">Hello.jpg</a></li>
<li><a href="files/test.txt">Test.jpg</a></li>
</ul>
</div>
这是您的 PHP 代码的修订版,请尝试将其与示例中提供的 CSS 一起使用。
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div {
text-align: center;
box-sizing: border-box;
border: 1px solid black;
width: 50%;
float:left;
}
</style>
</head>
<body>
<?php
$filetypes = ['txt','jpg']; //with the following code you'll be able to add other fileextensions as you need them
foreach ($filetypes as $ext) {
$files = glob('uploads/*.'.$ext.'');
echo '<div><ul>';
foreach($files as $file){
echo'<li><a href="'.$file.'">'.(explode('/',$file)[1]).'</a></li>';
}
echo '</ul></div>';
}
?>
</body>
</html>
关于php - 在 if() echo 语句中排列 <Div> 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40312509/