简体   繁体   中英

php nested while loop in smarty using foreach

I have the following PHP code.

$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);

Smarty code using 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}

Here, the Member Id in the nested foreach loop isn't getting displayed. I don't know what is the error I am making.

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);

SMARTY:

{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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM