这是一个非营利组织的网站代码,该组织在有需要的情况下赞助儿童;赞助商可以登录到他/她的用户帐户,每三个月查看一次可以下载的其教子的一系列文件(照片,信件等)。

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

例:

2016年第一季度

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

2016年第二季度

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

等等。

到目前为止的问题是,如果我从“ while($ updates-> fetch())”中删除了“ $ file statement”,则页面显示有序期间,或者显示所有期间的所有文件以及“ $”文件语句”,而不是“ $ 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模板中对其进行循环。

10-04 22:42
查看更多