简体   繁体   English

使用两个SQL表中的标题创建一个HTML表

[英]Create a HTML table with headings from two SQL Tables

I am trying to create a simple file upload system using PHP and MySQL. 我正在尝试使用PHP和MySQL创建一个简单的文件上传系统。 I have created a database with two tables in, structure below: 我创建了一个包含两个表的数据库,结构如下:

File Table 文件表

+---+------------------+--------------+
| # |       Name       |     Type     |
+---+------------------+--------------+
| 1 | file_id          | int          |
| 2 | file_name        | varchar      |
| 3 | file_size        | int          |
| 4 | file_type        | varchar      |
| 5 | folder           | int          |Foreign Key to folders.folder_id
| 6 | user_Id          | int          |
| 7 | upload_time      | timestamp    |
| 8 | modify_time      | timestamp    |
+---+------------------+--------------+

Folders Table 文件夹表

+---+------------------+--------------+
| # |       Name       |     Type     |
+---+------------------+--------------+
| 1 | folder_id        | int          |
| 2 | name             | varchar      |
| 3 | subfolder        | int          |
+---+------------------+--------------+

Files Query 文件查询

    SELECT * 
FROM   files 
       JOIN folders 
         ON folders.folder_id = files.folder 
ORDER  BY folders.subfolder ASC, 
          folders.folder_id ASC 

Folders Query 文件夹查询

SELECT * FROM folders

The tables both echoed into a HTML table. 这些表都回显到HTML表中。 To do this, I have two query runs happening separately, one which echoes the folders and displays them on the table, and the other echoes the files and lists them below the list of folders. 为此,我有两个单独的查询运行,一个回显文件夹并将它们显示在表格上,另一个回显文件并将它们列在文件夹列表下方。

Rather than listing all the folders and then all the files I want the files to be listed below their relevant folders. 而不是列出所有文件夹,然后列出我希望文件列在其相关文件夹下面的所有文件。

This is how my table is currently displayed: 这是我的表当前显示的方式:

Table Heading
-------------
folder
folder2
folder3
file1
file2
file3
file4

This is how I want it to be: 这就是我想要的方式:

Table Heading
-------------
folder
file1
file2
folder2
file3
folder3
file4

Prepared statements are designed to be executed multiple times in a loop, this is a good example of where to use one: 准备好的语句被设计为在循环中多次执行,这是一个使用一个的好例子:

<?php
// prepare the statement for use later
$folderId = 0;
$stmt = $connectionUploads->prepare("SELECT * FROM files WHERE folder = ?");
$stmt->bind_param("i", $folderId);

$queryGetFolders = "SELECT * FROM folders";
$resultGetFolders = mysqli_query($connectionUploads, $queryGetFolders);

if ($resultGetFolders->num_rows != 0) {
    // loop through the folder list
    while ($rowFolders = $resultGetFolders->fetch_assoc()) {
        $folderName = ucwords($rowFolders['name']);
        echo "<h1>$folderName</h1>";

        // put the new value in $folderId
        $folderId = $rowFolders['folder_id'];
        // execute the query
        $stmt->execute();
        $resultGetFiles = $stmt->get_result();
        if ($resultGetFiles->num_rows == 0) {
            echo "<p>No files</p>";
        else {
            // loop through the results as normal
            while ($rowFiles = $resultGetFiles->fetch_assoc()) {
                echo "<p>$rowFiles[file_name]</p>";
            }
        }
    }
}

Note there are no inherent security concerns with using "plain" database queries. 请注意,使用“普通”数据库查询没有固有的安全问题。 The problems arise when you're adding user-supplied inputs to the query string. 当您将用户提供的输入添加到查询字符串时,会出现问题。 Then prepared statements are a must. 然后准备好的陈述是必须的。 In addition to reducing overhead when querying in a loop (as above) they also sanitize user input automatically. 除了在循环中查询时减少开销(如上所述),它们还自动清理用户输入。

You can try creating two php functions to call the various database tables, for instance 例如,您可以尝试创建两个php函数来调用各种数据库表

function fetch($query){

    while($row = $query->fetch_array(MYSQLI_ASSOC)){
        $result[] = $row;
    }

    return $result;
}

function getArray($a, $b, $c, $d){
    $array = mysqli_fetch_array(mysqli_query($a, "SELECT * FROM $b WHERE $c = '$d' ORDER BY $c DESC LIMIT 1"));
    return $array; 
}

then use foreach iterator to get the associative array for each file, then using the folder column you can be able to get the array for each folder where the files are found. 然后使用foreach迭代器获取每个文件的associative array ,然后使用folder列,您可以获取找到文件的每个文件夹的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM