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 to loop a variable frim 1 to 10 using smarty.

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
amit.imcc
Smarty n00b


Joined: 08 Sep 2011
Posts: 2

PostPosted: Thu Sep 08, 2011 1:23 pm    Post subject: how to loop a variable frim 1 to 10 using smarty. Reply with quote

hi,

My code is as follows:

$smarty->assign('customer_review',$rating);
$smarty->display("display.html");

'$rating' of integer type and it gets its value from database.

My problem is as follows:

for example suppose 'customer_review' get a value as 3. now i want to run a loop on my display.html page from 1 to value of 'customer review' and print 3 Stars. if it gets a value of 5 then print 5 stars using same loop.

How can i use 'for' or 'while' loop to achieve this.is it possible to use for or while loop using smarty in html file?

Any suggestion is highly appreciated.

Thanks in advance.
Back to top
View user's profile Send private message Send e-mail
androidworkz
Smarty Rookie


Joined: 08 Aug 2011
Posts: 15

PostPosted: Tue Sep 13, 2011 7:10 pm    Post subject: Reply with quote

Like this:
Code:

<style>
#stars img {
    float: left;
    margin-right: 2px;
}
</style>

<div id="stars">
{while $customer_review > 0}
<img src="img/star.png" alt="{$customer_review--} star">
{/while}
</div>


Last edited by androidworkz on Tue Sep 13, 2011 7:29 pm; edited 1 time in total
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Tue Sep 13, 2011 7:41 pm    Post subject: Reply with quote

jacz wrote:
It is better so show an image like
$customer_review.jpg
(performance)


This is completely ill advised. embedding the same image on a page multiple times has no negative implications on performance. You load a single image, one request. done.

The alternative would be 10 images (for 10 stars). Do the math.

jacz wrote:
or use
{'*'|str_repeat:$customer_review}


that on the other hand even beats the {while} approach above. nice!
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Tue Sep 13, 2011 8:02 pm    Post subject: Reply with quote

jacz wrote:
10x 1.jpg with 1 star.
10 single Stars images - 10 Request's - One load - Request's cost time.


Dude… I don't want to get offensive… so, let's try this first:
please create an html file and place 100 <img src="foo.jpg"> in it. make sure the foo.jpg exists. open the developer console of your favourite browser and look at the network tab. Please notice, that foo.jpg has been loaded exactly ONCE.
_________________
Twitter
Back to top
View user's profile Send private message 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 -> Smarty Development 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