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

Array handling with negative indexes

 
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 -> Bugs
View previous topic :: View next topic  
Author Message
TommyD
Smarty Rookie


Joined: 20 Feb 2004
Posts: 18

PostPosted: Mon Mar 15, 2004 5:07 pm    Post subject: Array handling with negative indexes Reply with quote

Hey there!

Using Smarty 2.6.1 with PHP 4.3.4

The var_dump of my array I like to handle with Smarty looks like the following:

Code:
array(2) {
  ["foo"]=>
  string(3) "bar"
  ["navig"]=>
  array(2) {
    [-1]=>
    array(2) {
      ["id"]=>
      string(1) "7"
      ["unit_title"]=>
      string(10) "Previous page #11"
    }
    [1]=>
    array(2) {
      ["id"]=>
      string(1) "9"
      ["unit_title"]=>
      string(12) "Next page #13"
     }
  }
}


The "navig" part of the above array holds the previous/next info on a certain item. Obviously negative indexes are allowed in PHP and not handled as strings (see above), the following Smarty code gives me back an error message:

Code:
{$myarray.navig[-1].id}


This works without problems:

Code:
{$myarray.navig[1].id}


If you wonder why I use a negative index, its a db thing... I could easily make this positive, but I thought I should state this here since its supposed to be a bug =)

Greetings,
Tommy.
_________________
Administrator / Developer on http://www.musicmademe.com
- http://thomaskeller.biz
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Mar 16, 2004 12:29 am    Post subject: Reply with quote

Section does not behave exactly as a PHP for-loop and indexed values work not entirely as you are expecting. AFAIK, the following is true:

- sections are intended to step through "regular" indexed arrays -- there is limited support for using sections as generic for (counter) loops, but at best, that is a secondary consideration.

- section loops always start at 0 and are never negative

- you can access a negative index from an array, but only indirectly, eg: {assign var=temp value=-1} {$test[$temp]} would work while {$test[-1]} fails. Using dot notation is similar: {$test.$temp} works while {$test.-1} fails. Note that for zero and positive values, all methods should work equally well.

While negative indexes are sometimes (though rarely) used on db's to maximize key coverage, from my POV it is somewhat peculiar to see those values being passed on and persisting into other layers.
Back to top
View user's profile Send private message
TommyD
Smarty Rookie


Joined: 20 Feb 2004
Posts: 18

PostPosted: Tue Mar 16, 2004 8:27 am    Post subject: Reply with quote

Thanks for your answer... everything you've stated is true. I'm now returning positive values from db and it works.
_________________
Administrator / Developer on http://www.musicmademe.com
- http://thomaskeller.biz
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 -> Bugs 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