dongtui2029 2016-10-28 20:08 采纳率: 0%
浏览 60
已采纳

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

I'm trying to print out the files in a directory in php, and one to line up the text files in one list and jpg files in another(at the same horizontal height, side by side).

<!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>

I have the following files in the directory:

test.txt hello.txt test.jpg hello.jpg

Which gives me the output of: (in the middle of the screen

          test.jpg

          hello.jpg

test.txt

hello.txt

But I want something as such (in the middle of the page), where .txt files come first.

          test.txt   test.jpg

          hello.txt  hello.jpg

I've tried adding a css element of `float:left;' and then adding that class to the div's but that doesn't fix the problem.

I also tried using: display: inline-block; to no avail

  • 写回答

2条回答 默认 最新

  • dongzhizhai4070 2016-10-28 20:17
    关注

    Here you go

    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>

    Here's a revised edition of your PHP-code, try that together with the CSS provided in the example.

    <!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>
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)