Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

give table heading of looped objects date

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
andrewfo
Smarty n00b


Joined: 04 Feb 2015
Posts: 2

PostPosted: Wed Feb 04, 2015 1:09 am    Post subject: give table heading of looped objects date Reply with quote

Hi,

I am trying to loop through a collection of objects that have a date property and print them to a table, when the date changes I would like to echo out the current date as a heading.

I think to get it done I need to save a variable in a scope higher that the current loop that I can reference in the loop, If the date changes, change the variable and continue the loop until the next time the date changes. here is some example code

Code:

{* store the current day as "%d" *}
{assign var=currDay value='null' scope="global"}

<table>
    {foreach from=$branches item="ticket"}

        {if $smarty.get.form_sorting eq 'date_created'}

            {if $currDay neq $ticket.date_created|date_format:"%d"}
                {* reassign the currDay variable and print it *}
                {assign var=currDay value=$ticket.date_created|date_format:"%d" scope="global"}
                <tr><caption> {$currDay} </caption></tr>
            {/if}

        {/if}

        <tr>
            <td> {$ticket.stuff} </td>
            <td> {$ticket.otherStuff} </td>
        </tr>

    {/foreach}
</table>


This isn't working and I'm not sure I can do it, as all that I have tried is failing. I'm new at smarty so any help would be appreciated,

regards,

Andrew
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 04, 2015 2:15 am    Post subject: Reply with quote

1. You're using wrong syntax for foreach.
The syntax is "{foreach $array as [$key =>] $value}". Yes, same as PHP.
2. You're using wrong formatters for date.
date_format() is date() compatible, not strftime() compatible. (yet another brain fart of PHP developers…)
3. You can not use more than one <caption> per table!
Start reading standards before writing your code.
4. You're using bogus spaces in your template. If you need margins, add margins, but don't try to emulate margins with spaces. Your children will never forgive you.
Back to top
View user's profile Send private message
andrewfo
Smarty n00b


Joined: 04 Feb 2015
Posts: 2

PostPosted: Wed Feb 04, 2015 3:47 am    Post subject: Reply with quote

Thanks for the reply,

I will definitely look into all that you have said.

Didn't know about the one caption per table, cheers for that.

I think I have been looking at the old docs, thanks for the new foreach syntax, it's much more natural, I'm sure there are many more improvements that I am yet to discover.

$timestamp|date:"d" & $timestamp|date:"%d" isn't working for me. date_format is though.

spaces were only for the example as there is no syntax highlighting on this forum, so I think it's easier to read when separated out with spaces.

anyway ...turns out it was my fault, I was referencing the incorrect variable identifier. It now works as expected.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 04, 2015 4:27 am    Post subject: Reply with quote

I was only referring to placeholder compatibility, not the modifier name.
Glad you've sorted it now, though.
BTW, it's easier to use "z" instead of "d" modifier, much less chance for collision.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP