[SOLVED]Help with Smarty iterate operation

Hi,
I’m trying to edit the site.tpl template in /templates/index/

the html markup I’m implementing, requires that I MUST list all journals information in two

different locations. example:

`

`

`

`

…hence I tried to use the {iterate} operation seperately for the two blocks.
example:

`


{iterate from=journals item=journal}
  • {/iterate}

  • `

    `


    {iterate from=journals item=journal}
  • {/iterate}

  • `

    the first iterate operation works fine with desired results. the second block does nothing.
    I’d appreciate any suggestions to get things to work.

    Hi @samice,

    The {iterate} function usually works on DAOResultFactory objects, which are backed by database result sets. These can only be used once, and then they’re automatically closed. If you need to go through it again, I’d suggest converting the iterator object into an array, using the toArray function, on the PHP side.

    Regards,
    Alec Smecher
    Public Knowledge Project Team

    Hi @asmecher,

    thanks for the regular prompt replies.
    please not to sound lazy or anything, I’d be grateful if you gave more pointers as to how to go about doing this.

    I opened “DAOResultFactory.inc.php”, “JournalDAO.inc.php”, “ItemIterator.inc.php”, but couldn’t really wrap my head around what was going on.

    More help needed please.

    The function Alec referenced is a method of the DAOResultFactory:

    Note that site.tpl is just the template (View) of the actual PHP page action. The $journals variable is being populated here:

    Hi all,

    I apologise in advance if it’s not allowed to ask a question in someone else’s post. It’s just that I’m facing the same road block on my project. I am working on a similar presentation, where I need to implement the iterate statement twice on a page.

    I’m ok, but not really very good with php, so I don’t know how to implement the suggestions you’ve made in the comments above. Could someone please give the exact code to convert the iterator object to an array?

    @samice, please have you solved this?

    Hi all,

    Note that you should be able to tackle this directly in the templates. Something like this (untested):

    {assign var=someArray value=$someIterator->toArray()}
    

    Then you’ll have to convert {iterate} calls to {foreach} instead (note a few small but important details in usage, such as from=$someArray rather than from=someIterator (missing $ in the iterator case).

    Regards,
    Alec Smecher
    Public Knowledge Project Team

    1 Like

    hi all,

    my final solution before now was a little jquery hacking. I used the iterate statement once, then used .after statement to move the html elements to where I wanted them in the page. (I’m not proud of this) :smile:

    that was very untidy.

    thanks to @asmecher and @ctgraham for joining the pieces together, now I’m doing the right thing.
    The codes @asmecher suggested, work 100%. It’s been tested by me.

    hope this helps you too @mugambbo.
    cheers.

    1 Like

    It worked! It really worked! Thanks @asmecher. I owe you one. @samice, it helped a great deal. Thanks

    1 Like