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

Creating if/counter/foreach based on number instead of array

 
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
THO
Smarty n00b


Joined: 07 Jan 2022
Posts: 3

PostPosted: Fri Jan 07, 2022 1:45 pm    Post subject: Creating if/counter/foreach based on number instead of array Reply with quote

Hi everyone,

im looking for a way to create a counter/iteration with a repitition based on a number instead of an array. The result will be placed in an XML-File.

My shop will create something like:

$order.amount => x while x can be a number from 1 to n

If x is > 1 i want to have a repitition in my XML-output like this:

<ResourceSet Name="RunList">
<Resource>
<Part Run="Part-01" />
<RunList>
<FileSpec URL="file://wieapp00950/TestData/file_1.pdf" />
</RunList>
</Resource>
<Resource>
<Part Run="Part-02" />
<RunList>
<FileSpec URL="file://wieapp00950/TestData/file_2.pdf" />
</RunList>
</Resource>
</ResourceSet>

Is there a way to start an iteration or counter with max-number of $order.amount that i can use in a foreach-loop for the ResourceSet of my XML-file?

Thanks in advance
Back to top
View user's profile Send private message
THO
Smarty n00b


Joined: 07 Jan 2022
Posts: 3

PostPosted: Mon Jan 10, 2022 9:45 am    Post subject: Reply with quote

I have found a way to get the result i was looking for. But i suppose there is a smarter way, using a counter for the first part, so the foreach doesn't have to go through all items checking if the value is <= $order.amount.

My approach for now is:

Code:
{assign var=count value=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]}
                    {foreach from=$count item=item key=key}
                        {if $item <= $order.amount}
                        <Resource>
                            <Part Run="Part-{$item}" />
                                <RunList>
                                    <FileSpec URL="file://wieapp00950/TestData/file_{$item}.pdf" />
                                </RunList>
                        </Resource>
                        {/if}
                    {/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Jan 28, 2022 10:46 am    Post subject: Reply with quote

Don't know how useful this would be to your case, but here's an example I baked some time ago
https://www.smarty.net/forums/viewtopic.php?t=25658&highlight=cycle
Back to top
View user's profile Send private message
SlowFox
Smarty Regular


Joined: 02 Oct 2006
Posts: 55

PostPosted: Sat Jan 29, 2022 10:16 pm    Post subject: Reply with quote

Without having tested it, but why not

Code:

{for $item=1 to $order.amount}
      <Resource>
          <Part Run="Part-{$item}" />
              <RunList>
                  <FileSpec URL="file://wieapp00950/TestData/file_{$item}.pdf" />
              </RunList>
      </Resource>
{/for}
Back to top
View user's profile Send private message
THO
Smarty n00b


Joined: 07 Jan 2022
Posts: 3

PostPosted: Tue Feb 01, 2022 7:38 am    Post subject: Reply with quote

Works like a charm, thank you SlowFox!
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