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

How find out the value of the next index in foreach/section

 
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
OmJFJ
Smarty Rookie


Joined: 29 Jul 2016
Posts: 5

PostPosted: Fri Jul 29, 2016 4:01 pm    Post subject: How find out the value of the next index in foreach/section Reply with quote

Need to know the value of the next Index in the current loop.

my $array is:
Code:
array[0].amount = 10;
array[1].amount = 20;
array[2].amount = 30;



Code:

{section name=id loop=$array}
  var a = {$array[id.index].amount}; // a is 10
  var b = {$array[id.index_next].amount}; // not working
{/section}
Back to top
View user's profile Send private message
OmJFJ
Smarty Rookie


Joined: 29 Jul 2016
Posts: 5

PostPosted: Sat Jul 30, 2016 4:53 pm    Post subject: Reply with quote

that is simply Unbelievable!. I would just have the next "index" of an array. most programming languages its solved by [i + 1], why it is so complicated here?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Jul 30, 2016 9:30 pm    Post subject: Reply with quote

How do you know that your indices are numeric and sequential?... The application could pass anything to your template, and your task is to render it, nothing more.
In 99.(9)% of tasks, if you need to know an index in advance, you've not formulated your problem correctly and trying to solve it backwards.
But, if you are dead set on solving it exactly this way, no matter how stupid that is, then http://www.smarty.net/docs/en/language.function.assign.tpl (heed the warning!)
Code:
{foreach $array as $item}
{if $item@first && $item@last}
{assign "_key" -1}
{assign "_value" null}
{/if}
{if isset($_key)}CurItem: {$item@key} = {$item}
PrevItem: {$_key} = {$_value}{/if}
{assign "_key" $item@key}
{assign "_value" $item}
{/foreach}
Back to top
View user's profile Send private message
OmJFJ
Smarty Rookie


Joined: 29 Jul 2016
Posts: 5

PostPosted: Sun Jul 31, 2016 1:37 pm    Post subject: Reply with quote

thank you sir for your reply.

I understand your argument, but before I write a complex program I get a "syntax" error. I do not have great experience in "smarty", but looks matter how the array should be able to find the next index.

I tried your code, but I get a blank white page. (Syntax error)

My problem is this:

I have a tiered pricing and depending on how the input, you get a discount.
Standard Price: 12 $ per m
e.g: 10m = $ 10 per m / 20 m = 9 $ / 30 m = 8 $ / ... / 50 m = 6 $

my array of tiered pricing is: "{$Artikel->staffelPreis_arr}"
this has the following structure:
".nAnzahl" For scale quantity (10,20, ..) and
".cBasePriceLocalized[$NettoPreise]" for the Best Offers (10 $ 9 $, ..)

"summeMeter" is the input
"staffelMenge" is the current scale quantity in the loop
"staffelPreis" is actually renders Staffelpreis in the loop
"staffelMengeNext" to represent the next scale quantity
"endPreis" is the new price

Now comes my condition:

Code:

{foreach $Artikel->staffelPreis_arr as $differentialPrice key=id}
var staffelMenge = {$differentialPrice.nAnzahl};
var staffelMengeNext = {$Artikel->staffelPreis_arr[id.next_index].nAnzahl}; //doesn't work
var staffelPreis = "{$differentialPrice.cBasePriceLocalized[$NettoPreise]}";
staffelPreis = staffelPreis.replace(',','.');
staffelPreis = parseFloat(staffelPreis).toFixed(2);

if(summeMeter >=  staffelMenge && summeMeter < staffelMengeNext){
            endPreis = staffelPreis;
            return;
          }
{/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jul 31, 2016 8:05 pm    Post subject: Reply with quote

Let me guess, you are using Smarty2?...
Back to top
View user's profile Send private message
OmJFJ
Smarty Rookie


Joined: 29 Jul 2016
Posts: 5

PostPosted: Mon Aug 01, 2016 12:58 am    Post subject: Reply with quote

AnrDaemon wrote:
Let me guess, you are using Smarty2?...


yes.

I found the problem but i cant solve it. Its not working because I am writing inside a <script>, I have tested the code outside the <script> and its working. But I need the script for some variables. I tried many solutions for {literal} but not worked.

{foreach $array as $myArray name=arrayName}
{$nextValue=$array[$smarty.foreach.arrayName.index+1].item}
{/foreach}

but I want this inside my <script>
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