准备好的语句:while循环内圈。如何在组中显示有序文件?

时间:2017-03-06 21:13:39

标签: php mysql database mysqli prepared-statement

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

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

示例:

2016年第一季度

档案A1 :::档案B1 :::档案C1

2016年第二季度

档案A2 :::档案B2 :::档案C2

到目前为止的问题是,如果我从'while($ updates - > fetch())'中删除'$ file statement',页面会显示有序时段,或者显示所有时段的所有文件使用'$ 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>
        ';
...
?>

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效;您使用变量($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模板中循环它。