|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
amit.imcc Smarty n00b
Joined: 08 Sep 2011 Posts: 2
|
Posted: Thu Sep 08, 2011 1:23 pm Post subject: how to loop a variable frim 1 to 10 using smarty. |
|
|
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 |
|
androidworkz Smarty Rookie
Joined: 08 Aug 2011 Posts: 15
|
Posted: Tue Sep 13, 2011 7:10 pm Post subject: |
|
|
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 |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Tue Sep 13, 2011 7:41 pm Post subject: |
|
|
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 |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Tue Sep 13, 2011 8:02 pm Post subject: |
|
|
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 |
|
|
|
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
|