Search code examples
phparrayssmartysmarty3

Smarty array show matching string


Im not sure if im asking right but here is the question and it is simple:

I got an Smarty array with code

<td>
    {foreach from=$referrals item=referral}
        {$referral.service} - {$referral.service|@count}</br>
    {/foreach}
</td>

And the result is this: Screenshot of array result

My array is this:

	Smarty_Variable Object (3)
->value = Array (29)
  0 => Array (11)
    id => "1433"
    date => "25/12/2015"
    service => "Деловен именик"
    package => "Деловен именик"
    userid => "469"
    amount => "0.00"
    billingcycle => "One Time"
    amountdesc => "0,00 ден. One Time"
    commission => "0,00 ден."
    lastpaid => "Never"
    status => "Active"
  1 => Array (11)
    id => "1434"
    date => "25/12/2015"
    service => "Лого со податоци"
    package => "Лого со податоци"
    userid => "469"
    amount => "0.00"
    billingcycle => "One Time"
    amountdesc => "0,00 ден. One Time"
    commission => "0,00 ден."
    lastpaid => "Never"
    status => "Active"
  2 => Array (11)
    id => "1435"
    date => "25/12/2015"
    service => "Реклама во црно-бела техника на 1/6 -..."
    package => "Реклама во црно-бела техника на 1/6 -..."
    userid => "469"
    amount => "6160.00"
    billingcycle => "One Time"
    amountdesc => "6.160,00 ден. One Time"
    commission => "1.232,00 ден."
    lastpaid => "Never"
    status => "Active"
  3 => Array (11)
    id => "1436"
    date => "25/12/2015"
    service => "Лого со податоци"
    package => "Лого со податоци"
    userid => "470"
    amount => "0.00"
    billingcycle => "One Time"
    amountdesc => "1.848,00 ден. One Time"
    commission => "369,60 ден."
    lastpaid => "Never"
    status => "Active"
  4 => Array (11)
    id => "1437"
    date => "25/12/2015"
    service => "Деловен именик"
    package => "Деловен именик"
    userid => "471"
    amount => "0.00"
    billingcycle => "One Time"
    amountdesc => "0,00 ден. One Time"
    commission => "0,00 ден."
    lastpaid => "Never"
    status => "Active"

All i want to do is to display only array strings or value`s with name "Деловен именик" and rest from the array to hide. So at the end to have this kind of result: Screenshot final result -wanted


Solution

  • Have you tried adding an {if} into your {foreach}-loop?

    It might just be as easy as demonstraded here:

    <td>
        {foreach from=$referrals item=referral}
            {if $referral.service eq "Деловен именик"}
                {$referral.service} - {$referral.service|@count}</br>
            {/if}
        {/foreach}
    </td>
    

    However, this is a rather specialized approach, you'd have to edit the template if you want to filter something different. If this is the base for a more general solution, you'd be better off coding something in php, possibly another item in your array to filter.