php - Prepared statements : while loop inside while loop. 如何分组显示有序文件?

标签 php mysql database mysqli prepared-statement

这是一个为有需要的 child 提供资助的非营利组织网站的代码;赞助商可以登录到他/她的用户帐户,每三个月可以看到一系列可以下载的他教子的文件(照片、信件等)。

此代码应选择在名为“files”的表中注册的可用文件,并显示按相应时间段排序的结果,即三个月的四个时间段。

例子:

2016 年第一季度

存档 A1::: 存档 B1::: 存档 C1

2016 年第二季度

存档 A2::: 存档 B2::: 存档 C2

等等

到目前为止的问题是,如果我从“while ($updates -> fetch())”中删除“$file statement”,页面要么显示有序的周期,要么显示所有周期的所有文件'$file 语句' whitout '$updates 语句'。

有人可以帮帮我吗?

<?php
...

        // This relate the file with its user.
        $FileCode = $_SESSION["Card"];

        echo '
            <div class="contentLogIn">
                <div class="ctlg-ins">
                    <p class="startText">My files</p>
                </div>
        ';


        // I use SELECT DISTINCT because in the table 'files' multiple values can have the same period but i only want to know if the period have values not how much.

        $updates = $db->prepare("SELECT DISTINCT Trimester, Quarter, Year FROM files WHERE FileCode=? ORDER BY Year DESC, Quarter DESC, Trimester DESC");
        if ($updates === FALSE) {trigger_error($db->mysqli->error, E_USER_ERROR);}
        $updates->bind_param("s", $FileCode);
        $updates->execute();
        $updates->bind_result( $Trimester, $Quarter, $Year );
        while ( $updates->fetch() ) {


            // For every single period shows its title.

            if ( isset($Year, $Quarter) ) {
                $FilesDate = $Trimester . "&nbsp;quarter&nbsp;of&nbsp;" . $Year; // Output example: *First quarter of 2016*
            } else {
                $FilesDate = "There's no available files.";
            }
            echo '
                    <div class="W100">
                        <p class="startTextDesc">' . $FilesDate . '</p>
                    </div>
                    <div class="A12">
            ';


            $files = $db->prepare("SELECT Type, Class, SRC FROM files WHERE FileCode=? AND Quarter=? AND Year=?");
            if ($files === FALSE) { trigger_error($db->mysqli->error, E_USER_ERROR); }
            $files->bind_param("sss", $FileCode, $Quarter, $Year);
            $files->execute();
            $files->bind_result($Type, $Class, $SRC);
            while ( $files->fetch() ) {


                // This is the file that it is supposed to be displayed under its period title - like the example i post before.

                echo '
                        <div class="FileCont  CustomPointer">
                            <form method="POST" action="/php/File.Download.php">
                                <input type="hidden" name="fileButton" id="fileButton" value="' . $SRC . '">
                                <button type="submit" class="buttonFile-download">
                                    <div class="CustomPointer">
                                        <p style="...">' . $Type . '</p>
                                    </div>
                                </button>
                            </form>
                        </div>
                ';
            }
            $files->free_result();
            echo '
                    </div>
            ';
        }
        $updates->free_result();
        echo '
            </div>
        ';
...
?>

最佳答案

像这样的东西应该可以工作;您使用一个变量 ($CurrentFilesDate) 来跟踪时间段,当您看到它发生变化时,您可以放入一个新的标题。

$CurrentFilesDate = "";
$updates = $db->prepare("SELECT Trimester, Quarter, Year, Type, Class, SRC FROM files WHERE FileCode=? ORDER BY Year DESC, Quarter DESC, Trimester DESC");
if ($updates === FALSE) {
    trigger_error($db->mysqli->error, E_USER_ERROR);
}
$updates->bind_param("s", $FileCode);
$updates->execute();
$updates->bind_result( $Trimester, $Quarter, $Year, $Type, $Class, $SRC );
while ( $updates->fetch() ) {
    $FilesDate = $Trimester . "&nbsp;quarter&nbsp;of&nbsp;" . $Year;
    if ($FilesDate != $CurrentFilesDate) {
        if (!empty($CurrentFilesDate)) {
            echo '</div>';
        }
        echo '
                <div class="W100">
                    <p class="startTextDesc">' . $FilesDate . '</p>
                </div>
                <div class="A12">
        ';
        $CurrentFilesDate = $FilesDate;
    }
    echo '
            <div class="FileCont  CustomPointer">
                <form method="POST" action="/php/File.Download.php">
                    <input type="hidden" name="fileButton" id="fileButton" value="' . $SRC . '">
                    <button type="submit" class="buttonFile-download">
                        <div class="CustomPointer">
                            <p style="...">' . $Type . '</p>
                        </div>
                    </button>
                </form>
            </div>
    ';
}
echo '</div>';

虽然这段代码非常难看,但您应该将 PHP 和 HTML 分开。我建议使用 DB 循环在您需要的结构中构建一个数组,然后在仅包含简单 echo 语句的 HTML 模板中循环遍历它。

关于php - Prepared statements : while loop inside while loop. 如何分组显示有序文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42635675/

相关文章:

php - 数据在表中后,Laravel Migration 添加字段?

database - 存储二进制或图像文件的最佳方式

python - 如何在 Django 中删除? (mysql事务)

php - CodeIgniter-YouTube-API-Library 注册账号认证

php - 分析有关非转义查询等的 PHP 代码

php - 用于解析 .strings 文件的正则表达式

javascript - 我的 ajax 代码显示评论时出现问题

javascript - 如何调用 php 函数以使用 jQuery 从 MYSQL 获取更新的数据

javascript - Node.JS MySql 模块 - 无法连接到任何数据库 - ECONNRESET

mysql - 将数字限制为 MySQL 中的最大值