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

section looping cuts off the last object in the 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
Noah
Smarty n00b


Joined: 05 Jan 2004
Posts: 3

PostPosted: Mon Jan 05, 2004 7:06 am    Post subject: section looping cuts off the last object in the array! Reply with quote

While I am doing a section loop using the following code, whatever is the last item in the $rankCats array is not ever displayed. What's wrong?
(A quick rundown: This page displays user ranks. There is an array of the rank categories ($rankCats) and one of the actual ranks($ranks).

Code:

{section name=RankCats loop=$rankCats start=1}
<tr>
 <td colspan="2">
  <div align="center" class="header">{$rankCats[RankCats].rankCatName}</div>
 </td>
</tr>
{section name=ranks loop=$ranks}
{if $ranks[ranks].rankCatID == $rankCats[RankCats].rankCatID}
<tr>
 <td>
  {if $ranks[ranks].rankImage != ""}
   <div align="center"><img src="{$ranks[ranks].rankImage}" alt="{$ranks[ranks].rankTitle}" /></div>
  {/if}
 </td>
 <td width="100%">
  {$ranks[ranks].rankTitle}
 </td>
</tr>
{/if}
{/section}
{/section}


{$rankCats|@debug_print_var}
Generates the following output:
Code:

Array (5)
1 => Array (2)
  rankCatName => Leaders
  rankCatID => 1
2 => Array (2)
  rankCatName => Team Members
  rankCatID => 2
3 => Array (2)
  rankCatName => Community Members
  rankCatID => 3
4 => Array (2)
  rankCatName => Friends
  rankCatID => 4
5 => Array (2)
  rankCatName => Newbies
  rankCatID => 5

But 'Newbies' is not ever shown!
Back to top
View user's profile Send private message
Noah
Smarty n00b


Joined: 05 Jan 2004
Posts: 3

PostPosted: Tue Jan 06, 2004 8:51 pm    Post subject: Problem Solved! Reply with quote

I don't know if I misunderstood how it was supposed to work or if this is a bug, but when you are printing from an array using "start=1" then the last item in the array is cut off. (Assuming that the array starts at index [1]. if it starts at index [0] then the first element is not shown)
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Jan 06, 2004 10:42 pm    Post subject: Reply with quote

your array is supposed to have n elements with indices from 0...(n-1) .
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Noah
Smarty n00b


Joined: 05 Jan 2004
Posts: 3

PostPosted: Wed Jan 07, 2004 5:07 am    Post subject: Reply with quote

Yes, I've figured this out, but I was under the impression that "start=1" would start at index 1 (which it does). It also prevents the loop from displaying the last element of the array. The solution for the problem, therefor, is to make sure that all your arrays start at index 0. It still sounds like a bug to me, given that the documentation on the start attribute is as follows
Quote:

The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.

Nowhere in there does it refer to the elimination of array values.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jan 07, 2004 6:25 am    Post subject: Reply with quote

if your array indices are 0...5 (6 elements) and you set start=1 then the loop goes 1...5.

start tells smarty where the loop should start, not where the array should start. the array always starts at zero.

again: section only works if your arrays are zero-based and then nothing is cut off unexpectedly.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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