CodeIgniter Forums
Repeating an image according to a value from the database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Repeating an image according to a value from the database (/showthread.php?tid=39769)



Repeating an image according to a value from the database - El Forum - 03-20-2011

[eluser]victorche[/eluser]
I have a simple problem, which makes me crazy since yesterday. Let me show you the code:
Code:
$rows = $this->users->all_users(); // This gives me an array of all users.
One of the values here is the user rating, which is a round value from 1 to 10.
I just want to display a smile emoticon for the rating, instead of a number.
So, for example if an user has rating 8, I want to display one .gif 8 times. Like this (view file):
Code:
{rows}
<p>Username: {username}</p>
<p>Rating: {rating}<img src="emoticon.gif" />{/rating}</p>
{/rows}
Right now, I can display the rating as a number with this view:
Code:
{rows}
<p>Username: {username}</p>
<p>Rating: {rating}</p>
{/rows}
So, how can I make {rating} to become an array, which can repeat the image as many times, as the actual rating value?

Please, help me... As this makes me really crazy Sad


Repeating an image according to a value from the database - El Forum - 03-20-2011

[eluser]victorche[/eluser]
One more thing ... I know, I can replace the rating number with the images, using jQuery for example. But I want to manage with this in the code. Controller side, I mean.
So how can I repeat one image, when I have the repeating value from the database?

Please, give me a hint...


Repeating an image according to a value from the database - El Forum - 03-20-2011

[eluser]wh1tel1te[/eluser]
I don't think you can do that with pseudo-variables (template parsing). You will have to use straight PHP.

Code:
&lt;?php
foreach($rows as $row)
{
    ?&gt;
    <p>Username: &lt;?php echo $row['username']; ?&gt;</p>
    <p>Rating: &lt;?php
    for($i=0; $i<$row['rating']; $i++)
    {
        ?&gt;<img src="emoticon.gif" alt="" />&lt;?php
    }
    ?&gt;</p>
    &lt;?php
}
?&gt;



Repeating an image according to a value from the database - El Forum - 03-20-2011

[eluser]victorche[/eluser]
That's strange, I thought it would be really easy task. Anyway, thanks for your help! Maybe someone else can give a hint here...


Repeating an image according to a value from the database - El Forum - 03-20-2011

[eluser]wh1tel1te[/eluser]
The template parser class is very basic. All it can do is output variables, and loop through arrays. It cannot loop through a certain number as you want it to do. You will have to use straight PHP rather than pseudo-variables, as I suggested above.


Repeating an image according to a value from the database - El Forum - 03-20-2011

[eluser]victorche[/eluser]
[quote author="wh1tel1te" date="1300669753"]The template parser class is very basic. All it can do is output variables, and loop through arrays. It cannot loop through a certain number as you want it to do. You will have to use straight PHP rather than pseudo-variables, as I suggested above.[/quote]
Really thanks! This was a really useful information. And because I don't want php code in my templates... I found another solution. Thanks again!