简体   繁体   中英

Foreach statement in smarty php

I have the following:

    {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 of html:

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

I need it to return like this:

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

Array for: $invoices

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

Array for: $nfseData

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

The problem is that the return of from=$nfseData has more than one record, in which case it is duplicating the lines without following this rule: {if $items.title|substr:16:4 eq $invoice.id}

I need it to return only those values that are positive for this rule, that is, that have the same number.

Is there a possibility to put this rule directly in the foreach so that it returns only the lines that obey the rule? Or is there some other way?

You are looping over all items for each invoice. You are showing them whether they belong to that invoice or not. You can nest your status filter inside if which checks for invoice id and doesn't show them if not matches.

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

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