php 使用 foreach 在 smarty 中嵌套 while 循环
php nested while loop in smarty using foreach
我有以下 PHP 代码。
$stmt = $pdo->prepare("SELECT * FROM memberships WHERE mb_status = 'enabled'");
$stmt-> execute();
$mbData = array();
while($f = $stmt->fetch()){
$mbData[] = $f;
$mbsId = $f['mb_id'];
$stmtUpg = $pdo->prepare("SELECT * FROM upgrade_validities WHERE upval_membership = :mbs");
$stmtUpg-> bindValue(':mbs', $mbsId);
$stmtUpg-> execute();
$upgVal = array();
while($uf = $stmtUpg->fetch()){
$upgVal[] = $uf;
}
}
$smarty->assign('mbData', $mbData);
$smarty->assign('upgVal', $upgVal);
使用 foreach 的聪明代码
{foreach from=$mbData key=k item=b}
{$b.mb_name}
<!-- Listing Item -->
{foreach from=$upgVal key=k item=a}
<h3>Member Id: {$a.upval_id}</h3> // doesn't get displayed
{/foreach}
<!-- Listing Item / End -->
{/foreach}
此处,嵌套的 foreach 循环中的成员 ID 未显示。我不知道我犯了什么错误。
PHP:
$stmt = $pdo->prepare("SELECT * FROM memberships WHERE mb_status = 'enabled'");
$stmt-> execute();
$mbData = array();
while($f = $stmt->fetch()){
$mbData[] = $f;
$mbsId = $f['mb_id'];
$stmtUpg = $pdo->prepare("SELECT * FROM upgrade_validities WHERE upval_membership = :mbs");
$stmtUpg-> bindValue(':mbs', $mbsId);
$stmtUpg-> execute();
// change begin
$upgValTemp = array();
while($uf = $stmtUpg->fetch()){
$upgValTemp[] = $uf;
}
$upgVal[$mbsId] = $upgValTemp;
}
$smarty->assign('mbData', $mbData);
$smarty->assign('upgVal', $upgVal);
聪明:
{foreach from=$mbData key=k item=b}
{$b.mb_name}
<!-- Listing Item -->
{foreach from=$upgVal[$b.mb_id] key=k item=a} {* changed here *}
<h3>Member Id: {$a.upval_id}</h3>
{/foreach}
<!-- Listing Item / End -->
{/foreach}
我有以下 PHP 代码。
$stmt = $pdo->prepare("SELECT * FROM memberships WHERE mb_status = 'enabled'");
$stmt-> execute();
$mbData = array();
while($f = $stmt->fetch()){
$mbData[] = $f;
$mbsId = $f['mb_id'];
$stmtUpg = $pdo->prepare("SELECT * FROM upgrade_validities WHERE upval_membership = :mbs");
$stmtUpg-> bindValue(':mbs', $mbsId);
$stmtUpg-> execute();
$upgVal = array();
while($uf = $stmtUpg->fetch()){
$upgVal[] = $uf;
}
}
$smarty->assign('mbData', $mbData);
$smarty->assign('upgVal', $upgVal);
使用 foreach 的聪明代码
{foreach from=$mbData key=k item=b}
{$b.mb_name}
<!-- Listing Item -->
{foreach from=$upgVal key=k item=a}
<h3>Member Id: {$a.upval_id}</h3> // doesn't get displayed
{/foreach}
<!-- Listing Item / End -->
{/foreach}
此处,嵌套的 foreach 循环中的成员 ID 未显示。我不知道我犯了什么错误。
PHP:
$stmt = $pdo->prepare("SELECT * FROM memberships WHERE mb_status = 'enabled'");
$stmt-> execute();
$mbData = array();
while($f = $stmt->fetch()){
$mbData[] = $f;
$mbsId = $f['mb_id'];
$stmtUpg = $pdo->prepare("SELECT * FROM upgrade_validities WHERE upval_membership = :mbs");
$stmtUpg-> bindValue(':mbs', $mbsId);
$stmtUpg-> execute();
// change begin
$upgValTemp = array();
while($uf = $stmtUpg->fetch()){
$upgValTemp[] = $uf;
}
$upgVal[$mbsId] = $upgValTemp;
}
$smarty->assign('mbData', $mbData);
$smarty->assign('upgVal', $upgVal);
聪明:
{foreach from=$mbData key=k item=b}
{$b.mb_name}
<!-- Listing Item -->
{foreach from=$upgVal[$b.mb_id] key=k item=a} {* changed here *}
<h3>Member Id: {$a.upval_id}</h3>
{/foreach}
<!-- Listing Item / End -->
{/foreach}