[SOLVED] ahh! Smarty help please Modulus fustration!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

[SOLVED] ahh! Smarty help please Modulus fustration!

Post 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.
Last edited by DaveTheAve on Sun Jul 15, 2007 11:55 am, edited 5 times in total.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Is there any reason why the modulus number is different (5 and 4)? ;-)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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!
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post 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.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post 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.
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

Thanks smuge... that was exactly what the [s]server[/s] interpreter ordered.
Post Reply