【问题标题】:Prepared statements: while loop inside while loop. How to display ordered files in groups?准备好的语句:while 循环内的 while 循环。如何分组显示有序文件?
【发布时间】:2017-03-06 21:13:39
【问题描述】:

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

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

例子:

2016 年第一季度

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

2016 年第二季度

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

等等

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

【问题讨论】:

  • 一次准备,多次执行。或者,只需执行一次数据库查询并使用变量跟踪您的不同时期。
  • 确实,“$files 语句”执行了多次,但我这样做只是为了在此处发布更“可读”,但它也不起作用。
  • 我认为您的架构需要重新设计。见meta.stackoverflow.com/questions/333952/…

标签: php mysql database mysqli prepared-statement


【解决方案1】:

这样的事情应该可以;您使用一个变量 ($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 模板中循环遍历它。

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 2010-10-05
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多