Warm tip: This article is reproduced from stackoverflow.com, please click
crud html mysql php

How to show all files from DB horizontaly using php?

发布于 2020-04-16 12:18:30

I have Uploaded files to a directory and in database and i want to show the files in the browser and make the downloadable but on one file is showing in the browser. How it shows one file in browser. Here is how the database looks like Database. Here is the Directory of my Files Image Directory And the code that i wrote

<?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>";
             }
    }
?>

How can i make the images horizontally styled i.e movies.txt followed by procurement.pdf etc. like this Procument file should be under movies.txt.

Questioner
sehphirrydev
Viewed
28
CodyKL 2020-02-04 18:19

Define your table outside of the loop and inside the loop only the rows.

Example:

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;
}

Btw. Your code is vulnerable for SQL injections!