【发布时间】: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 . " quarter of " . $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