【问题标题】:How to show all files from DB horizontaly using php?如何使用php水平显示数据库中的所有文件?
【发布时间】:2020-05-20 03:16:57
【问题描述】:

我已将文件上传到目录和数据库中,我想在浏览器中显示文件并使其可下载,但只有一个文件显示在浏览器中。 How it shows one file in browser。这是数据库的样子Database。这是我的文件目录Image Directory 和我写的代码

<?php
     $Year = mysqli_query($link, "SELECT distinct `Year` FROM `returndocs` WHERE EmailAddress='$name'");
     $Returns = 'Returns';
     if($Year->num_rows>0)
         {
            while ($row=$Year->fetch_assoc())
                {
                   echo "<button type='button' class='collapsible'>
                           <div style='display:flex'>
                              <div><img src='../Assets/images/icons/folder-icon.png' width='18' height='18'></div>
                                <div style='margin-left:5px;'>".$row['Year'].' '.$Returns."</div>
                           </div>
                         </button>";
                }
          }
     else{
             echo "<h6 style='color:#bf9038;'>No returns Submitted!</h6>";
         }
// Viewing files from Db and from directory 
$SelectUploadedFiles = mysqli_query($link, "SELECT * FROM `returndocs` where EmailAddress='$name';");
if($SelectUploadedFiles->num_rows > 0)
   {
       while($file=$SelectUploadedFiles->fetch_assoc())
            {
               echo "<div class='content'>
                      <table>
                          <tr><a href=".$file['ReturnDir']." download>".$file['ReturnName']."</a></tr>
                      </table>
                  </div>";
             }
    }
?>

我怎样才能使图像水平样式,即movies.txt,然后是provisioning.pdf等。像这样Procument file should be under movies.txt

【问题讨论】:

    标签: php html mysql crud


    【解决方案1】:

    在循环外定义表格,在循环内只定义行。

    例子:

    if($SelectUploadedFiles->num_rows > 0) {
        // Define the div and table opening elements
        $ouput = '<div class="content"><table>';
        while($file=$SelectUploadedFiles->fetch_assoc()) {
            // Append for each entry a new table row
            $output .= '<tr><td><a href="' . $file['ReturnDir'] . '"> ' . $file['ReturnName'] . '</a></td></tr>';
        }
        // Close the table and div elements
        $output .= '</table></div>';
        // Show the table
        echo $output;
    }
    

    顺便说一句。您的代码易受 SQL 注入攻击!

    【讨论】:

    • 要在一行中显示文件(例如 file1.txt、file2.txt ...),只需将 &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; 移出循环并仅在循环内附加链接。 $output .= '&lt;a href="' . $file['ReturnDir'] . '"&gt; ' . $file['ReturnName'] . '&lt;/a&gt; ';
    【解决方案2】:

    我看到你得到了答案,但我想补充一些附加信息

    现在总是建议避免使用 HTML 中的表格,因为它们有很多问题,这些问题可以通过 ul、li 等其他 HTML 标记轻松处理。

    更多详情https://www.lifewire.com/dont-use-tables-for-layout-3468941

    如果您查看大多数现代网站,使用最多的是 divul,因为它们的代码更少、灵活且易于管理。同样,HTML 只是为了放置您的数据。

    这里你想水平显示它们,这应该是分配给CSS的任务。非常简单,您只需使用 flex 并提及您想要的方式,它会以最简单、最快捷的方式为您完成。

        ul li{
        display:block;
        text-decoration:none;
        }
    
        ul{
        display:flex;
        flex-direction: column;
        }
        
        .reverse {
        flex-direction:column-reverse;
        }
        <h4>Listing Horizontally</h4>
        <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
     </ul>
        <h4 >Listing Horizontally in Reverse</h4>
        <ul class="reverse">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
     </ul>

    就是这样..始终使用后端来获取数据并显示就这样。 如何显示是CSS

    希望这对你有意义......

    【讨论】:

    • 在此期间,将 html 与 php 混合是一种更糟糕的罪恶。至少十年来,我们拥有诱人的图书馆,人们还在哪里学习。
    • 我完全明白混合使用php和html是多么致命。我花了一段时间才明白这一点。只需使用 php 获取数据,将其放入必要的 html 标记,然后仅使用 CSS 来修复布局。否则在大规模情况下,这可能会导致性能问题。最好的部分是 CSS 被缓存,因此您的网站加载速度更快。
    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2013-12-09
    • 2017-06-24
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 2016-07-13
    相关资源
    最近更新 更多