如何在smarty foreach循环中排除最后一个数组元素的执行?

时间:2013-10-09 06:47:23

标签: php arrays foreach smarty associative-array

以下是一个名为$user_transactions的数组,它被分配给smarty模板:

Array
(
    [0] => Array
        (
            [transaction_user_id] => ce838192983c50e9f3c25663eee2d7bb
            [transaction_no] => 0349121204111
            [transaction_total_amount] => 918.00
            [transaction_date] => 11/04/2012
            [transaction_status] => cancelled
        )

    [1] => Array
        (
            [transaction_user_id] => 3ff1379851d1f3c01d2ade968cf3b3fa
            [transaction_no] => 1937101204132
            [transaction_total_amount] => 179.00
            [transaction_date] => 13/04/2012
            [transaction_status] => cancelled
        )

    [2] => Array
        (
            [transaction_user_id] => 9508feceeea6edb2cf12ad7eb600a315
            [transaction_no] => 1636011204143
            [transaction_total_amount] => 259.00
            [transaction_date] => 14/04/2012
            [transaction_status] => cancelled
        )

    [3] => Array
        (
            [transaction_user_id] => 3ff1379851d1f3c01d2ade968cf3b3fa
            [transaction_no] => 2309051204144
            [transaction_total_amount] => 179.00
            [transaction_date] => 14/04/2012
            [transaction_status] => cancelled
        )

    [4] => Array
        (
            [transaction_user_id] => 7e68b03b6c161fa230a9bc943012fb15
            [transaction_no] => 4606031204155
            [transaction_total_amount] => 956.00
            [transaction_date] => 15/04/2012
            [transaction_status] => cancelled
        )

    [5] => Array
        (
            [transaction_user_id] => 1b04818029044840c57879ddf5200538
            [transaction_no] => 1331081204176
            [transaction_total_amount] => 589.00
            [transaction_date] => 17/04/2012
            [transaction_status] => cancelled
        )

    [6] => Array
        (
            [transaction_user_id] => 4732fb946cb60d6e2073adbc1f3a5ca5
            [transaction_no] => 5909041204187
            [transaction_total_amount] => 589.00
            [transaction_date] => 18/04/2012
            [transaction_status] => cancelled
        )

    [7] => Array
        (
            [transaction_user_id] => 4732fb946cb60d6e2073adbc1f3a5ca5
            [transaction_no] => 4349041204188
            [transaction_total_amount] => 129.00
            [transaction_date] => 18/04/2012
            [transaction_status] => cancelled
        )

    [8] => Array
        (
            [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
            [transaction_no] => 2456081204259
            [transaction_total_amount] => 129.00
            [transaction_date] => 25/04/2012
            [transaction_status] => cancelled
        )

    [9] => Array
        (
            [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
            [transaction_no] => 39180912042510
            [transaction_total_amount] => 219.00
            [transaction_date] => 25/04/2012
            [transaction_status] => cancelled
        )

    [10] => Array
        (
            [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
            [transaction_no] => 58310212042511
            [transaction_total_amount] => 129.00
            [transaction_date] => 25/04/2012
            [transaction_status] => cancelled
        )

    [11] => Array
        (
            [transaction_user_id] => a5a24f328aad0f8053359ead49b83be9
            [transaction_no] => 16250912042512
            [transaction_total_amount] => 79.00
            [transaction_date] => 25/04/2012
            [transaction_status] => cancelled
        )

    [12] => Array
        (
            [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
            [transaction_no] => 35230912042613
            [transaction_total_amount] => 79.00
            [transaction_date] => 26/04/2012
            [transaction_status] => cancelled
        )

    [13] => Array
        (
            [transaction_user_id] => c1610997fa0741526d57c459bc6f961a
            [transaction_no] => 07300912042614
            [transaction_total_amount] => 219.00
            [transaction_date] => 26/04/2012
            [transaction_status] => cancelled
        )

    [14] => Array
        (
            [transaction_user_id] => 6dd8086fa23bad37580fe55f35509277
            [transaction_no] => 14430912042815
            [transaction_total_amount] => 589.00
            [transaction_date] => 28/04/2012
            [transaction_status] => cancelled
        )

    [15] => Array
        (
            [transaction_user_id] => e997536e693efa0ddd4ece248ba3430d
            [transaction_no] => 04000612042916
            [transaction_total_amount] => 589.00
            [transaction_date] => 29/04/2012
            [transaction_status] => cancelled
        )

    [16] => Array
        (
            [transaction_user_id] => c307ce45ef04ab1bf74592ab05792f75
            [transaction_no] => 28040912043017
            [transaction_total_amount] => 129.00
            [transaction_date] => 30/04/2012
            [transaction_status] => cancelled
        )

    [17] => Array
        (
            [transaction_user_id] => 359691e27b23f8ef3f8e1c50315cd506
            [transaction_no] => 19500912050218
            [transaction_total_amount] => 589.00
            [transaction_date] => 02/05/2012
            [transaction_status] => cancelled
        )

    [18] => Array
        (
            [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
            [transaction_no] => 36010512050819
            [transaction_total_amount] => 79.00
            [transaction_date] => 08/05/2012
            [transaction_status] => cancelled
        )

    [19] => Array
        (
            [transaction_user_id] => 9def02e6337b888d6dbe5617a172c18d
            [transaction_no] => 19020512050820
            [transaction_total_amount] => 299.00
            [transaction_date] => 08/05/2012
            [transaction_status] => cancelled
        )

    [transaction_count_data] => Array
        (
            [success_transaction_count] => 10
            [inprocess_transaction_count] => 11
            [cancelled_transaction_count] => 66
            [failed_transaction_count] => 0
        )

)

要在我的智能模板中打印我在forach循环中使用的数组元素:

<tbody>
  {if $user_transactions}           
    {foreach from=$user_transactions item='my_transaction'}
      <tr>
        <td>
          <div class="checkbxd"><input class="custom-check" type="checkbox" name="subject" id="subject"></div><div class="checkbxd_txt">{$my_transaction.transaction_no}</div>
        </td>
        <td>{$my_transaction.transaction_date}</td>
        <td>{$my_transaction.transaction_status|capitalize:true}</td>
        <td>{$my_transaction.transaction_total_amount}</td>
        <td align="left">
          <a href="{$control_url}modules/transactions/view_transactions.php?op=payment_details&page={$cur_page}&txn_no={$my_transaction.transaction_no}&user_id={$my_transaction.transaction_user_id}&from_date={$from_date}&to_date={$to_date}{if $transaction_status!=''}&transaction_status={$transaction_status}{/if}{if $transaction_no!=''}&transaction_no={$transaction_no}{/if}#searchPopContent" class="c-icn c-search inline_view_transaction_details">search</a>
        </td>
      </tr>
    {/foreach}  
  {else}
      <tr><td colspan="5" align="center"><b>You haven't made any transaction yet.</b></td></tr>
  {/if}  
  </tbody>

现在,我不希望从具有键transaction_count_data的数组中访问最后一个元素,因为它具有与其他数组元素不同的元素集。你能帮助我在smarty循环中排除foreach循环中的最后一个元素吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

我同意HamZa,因为这应该在它到达模板之前完成。也就是说,可以在Smarty中使用foreach.last属性。

{if not $smarty.foreach.my_transaction.last}do your stuff{/if}

答案 1 :(得分:0)

您可以使用last属性

{if $smarty.foreach.my_transaction.last}

<强> Reference