Page 1 of 1

[SOLVED] ahh! Smarty help please Modulus fustration!

Posted: Fri Jul 13, 2007 11:36 am
by DaveTheAve
I'm developing a photoalbum (obvious looking at the code) and I only want five photos a div.thinborder. May I please have some help?

Code: Select all

<div id='photoalbum'>

	{section name=i loop=$pictures}

		{if $smarty.section.i.iteration % 5 == 1}

			<div class='thinborder'>

		{/if}

		/uploads/photoalbums/{$album.album_id}/{$pictures[i][0]}

		{if $smarty.section.i.iteration % 4 == 1}

			</div>

		{/if}

	{/section}

</div>
Whats it printing? Well, obviously not what I want it too:

Code: Select all

<div class='thinborder'>
	/uploads/photoalbums/1/1.jpg
</div>
	/uploads/photoalbums/1/2.jpg
	/uploads/photoalbums/1/3.jpg
	/uploads/photoalbums/1/4.jpg
	/uploads/photoalbums/1/5.jpg
</div>
<div class='thinborder'>
	/uploads/photoalbums/1/6.jpg
	/uploads/photoalbums/1/7.jpg
	/uploads/photoalbums/1/8.jpg
	/uploads/photoalbums/1/9.jpg
</div>
P.S. This issue makes me feel like a bad programmer..... I know I'm not but I can NEVER understand this concept.... so if anyone has some good material to read that can help me understand issues similar to these I would really enjoy reading them.

Posted: Fri Jul 13, 2007 11:50 am
by Ambush Commander
Is there any reason why the modulus number is different (5 and 4)? ;-)

Posted: Fri Jul 13, 2007 11:55 am
by RobertGonzalez
Here's what you are doing, with comments:

Code: Select all

<div id='photoalbum'> 
   {* start a looping section named i *}
   {section name=i loop=$pictures} 
      {* If the iteration index is divisible by five, open a div *}
      {* We are going to call this div 'thinborder' *}
      {* This will only be opened if the iteration index is divisible by five *}
      {* Otherwise it will show now opening div *}
      {if $smarty.section.i.iteration % 5 == 1} 

         <div class='thinborder'> 

      {/if} 

      /uploads/photoalbums/{$album.album_id}/{$pictures[i][0]} 

      {* If the iteration index is divisible by 4, output a closing div *}
      {* Logically, since this iteration index check is not the same *}
      {* as the opening iteration index above, these two are totally unrelated *}
      {if $smarty.section.i.iteration % 4 == 1} 

         </div> 

      {/if} 

   {/section} 

</div>

Posted: Fri Jul 13, 2007 12:14 pm
by DaveTheAve
The only reason why those two are 5 & 4 is because if they where both the same it would OPEN & CLOSE the new div including ONLY ONE picture not the full five.

Posted: Fri Jul 13, 2007 12:26 pm
by RobertGonzalez
DaveTheAve wrote:The only reason why those two are 5 & 4 is because if they where both the same it would OPEN & CLOSE the new div including ONLY ONE picture not the full five.
Huh? :?

It is looping the entire set of data and throwing that data to the screen. If you want every fifth iteration to show something, then show it per 5th iteration.

Posted: Fri Jul 13, 2007 12:31 pm
by DaveTheAve
No offense, but I think you should see what happens if I do what your implying:

Code: Select all

<div id='photoalbum'>
        {section name=i loop=$pictures}
                {if $smarty.section.i.iteration % 5 == 1}
                        <div class='thinborder'>
                {/if}
                /uploads/photoalbums/{$album.album_id}/{$pictures[i][0]}
                {if $smarty.section.i.iteration % 5 == 1}
                        </div>
                {/if}
        {/section}
</div> 
gives me this:

Code: Select all

<div class='thinborder'>
        /uploads/photoalbums/1/1.jpg
</div>
        /uploads/photoalbums/1/2.jpg
        /uploads/photoalbums/1/3.jpg
        /uploads/photoalbums/1/4.jpg
        /uploads/photoalbums/1/5.jpg
<div class='thinborder'>
        /uploads/photoalbums/1/6.jpg
</div>
        /uploads/photoalbums/1/7.jpg
        /uploads/photoalbums/1/8.jpg
        /uploads/photoalbums/1/9.jpg

As you can see.... that is also NOT what I'm looking for.

Posted: Fri Jul 13, 2007 1:31 pm
by RobertGonzalez
I see what you want to do. You need to open the div at five and close it at 10, or the last record, if the last record index of the current group is less than 10.

Posted: Fri Jul 13, 2007 3:14 pm
by Ambush Commander

Code: Select all

<div id='photoalbum'>
  <div class='thinborder'>
        {section name=i loop=$pictures}
                /uploads/photoalbums/{$album.album_id}/{$pictures[i][0]}
                {if $smarty.section.i.iteration % 5 == 1}
                        </div>
                        <div class='thinborder'>
                {/if}
        {/section}
  </div>
</div>
The only problem with the above code (besides the fact that it's untested) is that there will always be a div even when there are no photos.

Posted: Fri Jul 13, 2007 3:23 pm
by RobertGonzalez

Code: Select all

<div id='photoalbum'> 
  {if count($pictures) gt 0}
  <div class='thinborder'> 
        {section name=i loop=$pictures} 
                /uploads/photoalbums/{$album.album_id}/{$pictures[i][0]} 
                {if $smarty.section.i.iteration % 5 == 1} 
                        </div> 
                        <div class='thinborder'> 
                {/if} 
        {/section} 
  </div> 
  {/if}
</div>
Now it will only show if there are at least 1 record.

Posted: Fri Jul 13, 2007 4:10 pm
by DaveTheAve
Thank you guys, I'm currently off to a bible-study, thus i can't test any of your code. However, I that last segment of code looks exactly like what i need. Thank you guys!

P.S. In the credits of this free, open-source project PHPDN will be there!

Posted: Fri Jul 13, 2007 9:34 pm
by DaveTheAve
Close but not quite:

Output:

Code: Select all

<div id='photoalbum'>
	<div class='thinborder'>
		/uploads/photoalbums/1/1.jpg
	</div>

	<div class='thinborder'>
		/uploads/photoalbums/1/2.jpg
		/uploads/photoalbums/1/3.jpg
		/uploads/photoalbums/1/4.jpg
		/uploads/photoalbums/1/5.jpg
		/uploads/photoalbums/1/6.jpg
	</div>

	<div class='thinborder'>
		/uploads/photoalbums/1/7.jpg
		/uploads/photoalbums/1/8.jpg
		/uploads/photoalbums/1/9.jpg
	</div>
</div>
Note: The first one is still by itself.

Posted: Fri Jul 13, 2007 9:58 pm
by smudge
I believe that if you want to check to see if x is evenly divisible by y, it's x%y==0.
By saying i%5==1, you are saying, if i divided by 5 leaves a remainder of 1, do this.
Try changing the 1 to 0 and see what it does.

Posted: Sun Jul 15, 2007 11:56 am
by DaveTheAve
Thanks smuge... that was exactly what the [s]server[/s] interpreter ordered.