php - 在 if() echo 语句中排列 <Div> 标签

标签 php html css

我正在尝试打印出 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/

相关文章:

php - PHP 变量的媒体查询

jquery - 如果内容增加,如何将嵌套的 div 容器向左移动

css - 如何在 Extjs 6 中包含自定义 css?

php - Mediawiki 回滚机器人(大规模撤消巨魔行动!)

PHP MVC 从 View 调用 Controller 函数

php - 哪种方法可以更好地防止 XSS?

javascript - 如何删除附加到滚动事件的行为?

javascript - Bootstrap 轮播,修改为显示多个元素。 Firefox 和 IE 无法正常工作

iphone - 在 iPhone 的 CSS 中更正边距或填充 - Wordpress 的 TouchFolio 主题

php - 如何在Windows服务器上使用.htaccess文件