I finished off my last post by sharing a Perl script which will output the number of stars for an application given the app id. Today I will explain what I hoped to accomplish when I came up with the idea and then the direction in which I ended up moving. Feel free to grab the script and use it for your own purposes.
For the sake of demonstration, let’s look at an example using 3½ stars.
UPDATE:
I obviously neglected to cover an important point when I posted this. For this to work, I will need to modify our Perl script to return the number of half stars. I did that in my script with
print $result*2;
but I will add an option for this in Friday’s post.
Here is the html for the stars:
<img src="images/star_on.gif" alt="Star" />
<img src="images/star_on.gif" alt="Star" />
<img src="images/star_on.gif" alt="Star" />
<img src="images/star_half.gif" alt="Half Star" />
<img src="images/star.gif" alt="No Star" />
We’re using PHP for this site so if I have the number of half stars, I can set up something like this:
<?php
$stars='7';
$half=$stars%2;
$stars=floor($stars/2);
for($i=0;$i<5;$i++) {
$thisimg=($stars>0)?'star_on':'star';
$t=($star=='star_on')?'':'No';
if ($thisimg=='star' && $half==1) {
$thisimg='star_half';
$t='Half';
$half=0;
}
echo "<img src=\"images/$thisimg.gif\" alt=\"$t Star\" />\n";
$stars--;
}
?>
So, now all I have to do is pull the star number before I do that and I’m all set. We replace this line
$stars='7';
With these two
$url=http://mydomain.com/path/to/script/getstars.cgi?id=304570595
$stars=file_get_contents($url);
So the whole thing looks like this
<?php
$url=http://mydomain.com/path/to/script/getstars.cgi?id=304570595
$stars=file_get_contents($url);
$half=$stars%2;
$stars=floor($stars/2);
for($i=0;$i<5;$i++) {
$thisimg=($stars>0)?'star_on':'star';
$t=($thisimg=='star_on')?'':'No';
if ($thisimg=='star' && $half==1) {
$thisimg='star_half';
$t='Half';
$half=0;
}
echo '<img src="images/$thisimg.gif" alt="$t Star" />\n';
$stars--;
}
?>
If you have any experience with this sort of thing, you will know that there are a couple of things to consider. The first is that it may make more sense to return the html instead of the number in the first place and include that html in the page. This is very easy to do in PHP. The second, more important, consideration is that this will call the getstars script AND request data from the iTunes store every time someone visits our page. I wanted to post this info today in case any of you can benefit from it, but I will cover the first of these considerations tomorrow.
This is part of a series of posts which start here. The next post is here.