smarty 中的 foreach 语句 php

Foreach statement in smarty php

我有以下内容:

    {foreach key=num item=invoice from=$invoices}
    <tr>
        <td class="text-center"><a href="viewinvoice.php?id={$invoice.id}">#{$invoice.invoicenum}</a></td>
        <td>{$invoice.datecreated}</td>
        <td>{$invoice.datedue}</td>
        <td data-order="{$invoice.totalnum}">{$invoice.total}</td>
        <td class="text-center">{include file="$template/includes/status-pages/status-faturas.tpl"}</td>
        <td class="text-center">
            {if $invoice.rawstatus eq 'paid'}
                {foreach from=$nfseData item=items}
                    {if $items.title|substr:16:4 eq $invoice.id && in_array($items.status, ['Answered', 'Closed'])}
                            <a href="{$WEB_ROOT}/dl.php?type=a&id={$items.id}&i=0" class="btn-sm rounded p-2 btn-success has-ripple"><i class="feather mr-1 icon-download"></i> Download NFS-e</a>
                        {elseif !in_array($items.status, ['Answered', 'Closed'])}
                            <a href="#!" class="btn-sm rounded p-2 btn-secondary has-ripple disabled"><i class="feather icon-refresh-cw mr-1"></i> Em processamento</a>
                            
                        {else}
                            <a href="{$WEB_ROOT}/submitticket.php?step=2&deptid=27&subject=NFS-e%20|%20Fatura%20#xxx%20|%20Valor%20R$xxxx" class="btn-sm rounded p-2 btn-primary has-ripple"><i class="feather icon-layout mr-1"></i> Solicitar NFS-e</a>
                        {/if}
                {/foreach}
            {else}
                <strong>N/D</strong>
            {/if}
        </td>
        <td class="text-center"><a href="viewinvoice.php?id={$invoice.id}" class="btn-sm rounded p-2 btn-info has-ripple"><i class="feather mr-1 icon-eye"></i> Ver fatura</a></td>
    </tr>
{/foreach}

Return 共 html:

<div>
    <span>No - 1030</span>
    <span>Yes - 1020</span>
</div>
<div>
    <span>No - 1030</span>
    <span>No - 1020</span>
</div>

我需要这样 return:

<div>
    <span>Yes - 1020</span>
</div>
<div>
    <span>No - 1030</span>
</div>

数组: $invoices

Array (4)
0 => Array (13)
  id => 1020
  rawstatus => "paid"
1 => Array (13)
  id => 1030
  rawstatus => "unpaid"

数组: $nfseData

Array (2)
0 => Array (27)
  id => 6446
  title => "NFS-e | Fatura #1020 | Valor R,90"
1 => Array (27)
  id => 6445
  title => "NFS-e | Fatura #1030 | Valor R,90"

问题是 from=$nfseData 的 return 有多个记录,在这种情况下,它会重复行而不遵循此规则:{if $items.title|substr:16:4 eq $invoice.id}

我需要它 return 只有那些对这个规则来说是正的值,也就是说,有相同的数字。

是否可以将此规则直接放在 foreach 中,以便 return 只有遵守该规则的行?或者有其他方法吗?

您正在遍历每张发票的所有项目。您正在向他们展示他们是否属于 invoice。您可以将 status 过滤器嵌套在 if 中,它会检查 invoice id,如果不匹配则不显示它们。

{foreach key=num item=invoice from=$invoices}
    <tr>
        <td class="text-center"><a href="viewinvoice.php?id={$invoice.id}">#{$invoice.invoicenum}</a></td>
        <td>{$invoice.datecreated}</td>
        <td>{$invoice.datedue}</td>
        <td data-order="{$invoice.totalnum}">{$invoice.total}</td>
        <td class="text-center">{include file="$template/includes/status-pages/status-faturas.tpl"}</td>
        <td class="text-center">
            {if $invoice.rawstatus eq 'paid'}
                {foreach from=$nfseData item=items}
                    {if $items.title|substr:16:4 eq $invoice.id}
                        {if in_array($items.status, ['Answered', 'Closed'])}
                            <a href="{$WEB_ROOT}/dl.php?type=a&id={$items.id}&i=0" class="btn-sm rounded p-2 btn-success has-ripple"><i class="feather mr-1 icon-download"></i> Download NFS-e</a>
                        {elseif $items.status eq 'Open'}
                            <a href="#!" class="btn-sm rounded p-2 btn-secondary has-ripple disabled"><i class="feather icon-refresh-cw mr-1"></i> Em processamento</a>
                            
                        {else}
                            <a href="{$WEB_ROOT}/submitticket.php?step=2&deptid=27&subject=NFS-e%20|%20Fatura%20#xxx%20|%20Valor%20R$xxxx" class="btn-sm rounded p-2 btn-primary has-ripple"><i class="feather icon-layout mr-1"></i> Solicitar NFS-e</a>
                        {/if}
                    {/if}
                {/foreach}
            {else}
                <strong>N/D</strong>
            {/if}
        </td>
        <td class="text-center"><a href="viewinvoice.php?id={$invoice.id}" class="btn-sm rounded p-2 btn-info has-ripple"><i class="feather mr-1 icon-eye"></i> Ver fatura</a></td>
    </tr>
{/foreach}