我遇到存储过程(MariaDB)的输出顺序问题。我尝试了几种不同的方法,每个回显的结果集都表现出同样的问题。 (我无法使用procedural / mysqli解决它,所以在PDO中重新制作项目,现在出现了同样的问题。)这是史诗般的,所以感谢所有读者。
我正在尝试为结果集中的每个成员创建一个表单信函报告,其中包含一个介绍和一个归属于该成员的费用表。
所以我希望看到:
会员1介绍,
成员1的表;
会员2介绍,
会员2的表;等等。
我遇到的问题是:
会员1介绍,
会员2介绍,
会员1表,
会员3介绍,
会员2表,
会员4介绍,
会员3表;等等。
调用存储过程:
SELECT * FROM `View_Accounts_report` WHERE `Date` >= parameter_start_date AND `Date` <= parameter_end_dated
调用html中的输出函数:
if(isset($_REQUEST['submit'])) {
Accounts_by_date($_REQUEST['start_date'], $_REQUEST['end_date']);
}
php函数Accounts_by_date方法1:
function Accounts_by_date($start_date, $end_date) {
require '../lib/pdo.php';
$prev = NULL;
$sql = "CALL accounts_report_dates (:start_date, :end_date)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':start_date', $start_date);
$stmt->bindValue(':end_date', $end_date);
$stmt->execute();
$results = $stmt->fetchALL(PDO::FETCH_ASSOC);
var_dump($results);
$arrlength = count($results);
for ($row = 0; $row < $arrlength; $row++) {
$person = $results[$row]['Member_ID']; // tracking each row
if ($person != $prev) { // if the current member is different from the previous member, print header and row
// table headers
echo "<br><br>Member: " . $results[$row]['Member_name'] . "<br>";
echo "Dear " . $results[$row]['First_name'] . ",<br>";
echo "Please find your Belmont Pottery Group fees for the period " . $start_date . " to " . $end_date . " below. <br>";
echo "<br><table><tr><th>Total</th><th>Member_ID</th><th>Date</th><th>Transaction</th><th>Amount</th>";
echo "<th>First name</th><th>Email</th><th>Member name</th></tr>";
// table rows
echo "<tr><td>" . $results[$row]['Total'] . "</td>";
echo "<td>" . $results[$row]['Member_ID'] . "</td>";
echo "<td>" . $results[$row]['Date'] . "</td>";
echo "<td>" . $results[$row]['Transaction'] . "</td>";
echo "<td>$" . $results[$row]['Amount'] . "</td>";
echo "<td>" . $results[$row]['First_name'] . "</td>";
echo "<td>" . $results[$row]['Email'] . "</td>";
echo "<td>" . $results[$row]['Member_name'] . "</td></tr>";
$prev = $person;
}else{
echo "<tr><td>" . $results[$row]['Total'] . "</td>";
echo "<td>" . $results[$row]['Member_ID'] . "</td>";
echo "<td>" . $results[$row]['Date'] . "</td>";
echo "<td>" . $results[$row]['Transaction'] . "</td>";
echo "<td>$" . $results[$row]['Amount'] . "</td>";
echo "<td>" . $results[$row]['First_name'] . "</td>";
echo "<td>" . $results[$row]['Email'] . "</td>";
echo "<td>" . $results[$row]['Member_name'] . "</td></tr>";
$prev = $person;
}
}
}
php函数方法1的var_dump摘录:
array(15){[0] =&gt; array(8){[“Total”] =&gt; string(5)“36.00”[“Member_ID”] =&gt; int(31)[“Date”] =&gt; string(10)“2017-11-06”[“Transaction”] =&gt; string(4)“Clay”[“Amount”] =&gt; string(5)“18.00”[“First_name”] =&gt; string(6)“Dummy”[“Email”] =&gt; string(25)“dummy@dummy.com”[“Member_name”] =&gt; string(12)“Dummy Nine”} [1] =&gt; array(8){[“Total”] =&gt; string(5)“36.00”[“Member_ID”] =&gt; int(31)[“Date”] =&gt; string(10)“2017-11-13”[“Transaction”] =&gt; string(4)“Clay”[“Amount”] =&gt; string(5)“18.00”[“First_name”] =&gt; string(6)“Dummy”[“Email”] =&gt; string(25)“dummy@dummy.com”[“Member_name”] =&gt; string(12)“Dummy Nine”} [2] =&gt; array(8){[“Total”] =&gt; string(5)“40.20”[“Member_ID”] =&gt; int(29)[“Date”] =&gt; string(10)“2017-11-05”[“Transaction”] =&gt; string(6)“Firing”[“Amount”] =&gt; string(4)“3.70”[“First_name”] =&gt; string(6)“Dummy”[“Email”] =&gt; string(25)“dummy@dummy.com”[“Member_name”] =&gt; string(13)“Dummy Seven”} [3] =&gt; array(8){[“Total”] =&gt; string(5)“40.20”[“Member_ID”] =&gt; int(29)[“Date”] =&gt; string(10)“2017-11-05”[“Transaction”] =&gt; string(10)“Attendance”[“Amount”] =&gt; string(4)“4.00”[“First_name”] =&gt; string(6)“Dummy”[“Email”] =&gt; string(25)“dummy@dummy.com”[“Member_name”] =&gt; string(13)“Dummy Seven”}
php函数Accounts_by_date方法2:
function Accounts_by_date($start_date, $end_date) {
require '../lib/pdo.php';
$prev = NULL;
$sql = "CALL accounts_report_dates (:start_date, :end_date)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':start_date', $start_date);
$stmt->bindValue(':end_date', $end_date);
$stmt->execute();
$stmt->bindColumn('Total', $total);
$stmt->bindColumn('Member_ID', $id);
$stmt->bindColumn('Date', $date);
$stmt->bindColumn('Transaction', $transaction);
$stmt->bindColumn('Amount', $amount);
$stmt->bindColumn('First_name', $fname);
$stmt->bindColumn('Email', $email);
$stmt->bindColumn('Member_name', $name);
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
$person = $id;
if ($person != $prev) { // if the current member is different from the previous member, print header and row
// table headers
echo "<br><br>Member: " . $name . "<br>";
echo "Dear " . $fname . ",<br>";
echo "Please find your Belmont Pottery Group fees for the period " . $start_date . " to " . $end_date . " below. <br><br>";
echo "The total of your invoice is: $" . $total . "<br>";
echo "<br><table><tr><th>Total</th><th>Member_ID</th><th>Date</th><th>Transaction</th><th>Amount</th>";
echo "<th>First name</th><th>Email</th><th>Member name</th></tr>";
// table rows
echo "<tr><td>" . $total . "</td>";
echo "<td>" . $id . "</td>";
echo "<td>" . $date . "</td>";
echo "<td>" . $transaction . "</td>";
echo "<td>$" . $amount . "</td>";
echo "<td>" . $fname . "</td>";
echo "<td>" . $email . "</td>";
echo "<td>" . $name . "</td></tr>";
$prev = $person;
}else{
echo "<tr><td>" . $total . "</td>";
echo "<td>" . $id . "</td>";
echo "<td>" . $date . "</td>";
echo "<td>" . $transaction . "</td>";
echo "<td>$" . $amount . "</td>";
echo "<td>" . $fname . "</td>";
echo "<td>" . $email . "</td>";
echo "<td>" . $name . "</td></tr>";
$prev = $person;
}
}
}
php函数方法2的var_dump返回true。
输出(两种方法):
会员:假九 亲爱的假人, 请在2017-11-01至2017-11-30期间查找您的BPG费用。
成员:Dummy Seven 亲爱的假人, 请在2017-11-01至2017-11-30期间查找您的BPG费用。
总会员数_ID日期交易金额名字电子邮件会员名称 36.00 31 2017-11-06粘土$ 18.00 Dummy dummy@dummy.com假九 36.00 31 2017-11-13 Clay $ 18.00 Dummy dummy@dummy.com Dummy Nine
成员:Dummy Six 亲爱的假人, 请在2017-11-01至2017-11-30期间查找您的BPG费用。
总会员数_ID日期交易金额名字电子邮件会员名称 40.20 29 2017-11-05射击$ 3.70 Dummy dummy@dummy.com Dummy Seven 40.20 29 2017-11-05出勤$ 4.00 Dummy dummy@dummy.com Dummy Seven 40.20 29 2017-11-19射击$ 11.60 Dummy dummy@dummy.com Dummy Seven
成员:假三 亲爱的假人, 请在2017-11-01至2017-11-30期间查找您的BPG费用。
总会员数_ID日期交易金额名字电子邮件会员名称 26.60 28 2017-11-02出勤$ 4.00 Dummy dummy@dummy.com假六 26.60 28 2017-11-06出勤$ 4.00 Dummy dummy@dummy.com假六 26.60 28 2017-11-03射击$ 17.00 Dummy dummy@dummy.com假六 26.60 28 2017-11-06 Firing $ 1.60 Dummy dummy@dummy.com Dummy Six
答案 0 :(得分:1)
首先确保关闭表格,然后添加以下代码:
if ($person != $prev) {
if ($row > 0) echo "</table>"; /*added line of code*/
这应解决同一个表中多个表头单元格的一些问题。一张表中的多个表单正在将您的介绍推到最顶层。
别忘了添加
</table>
到代码的结尾以及while循环完成正确的html后