Aardvark Topsites PHP Manual

Written By Jeremy Scheff - http://www.aardvarkind.com/

Stat Buttons


Stat Buttons are a cool new feature for Aardvark Topsites PHP, but you'll have to get your hands a little dirty to use them. This will be much easier if you know a little PHP.

Normally, you can only display a member's rank on their button. Stat Buttons allow you to display not only the rank, but their hits in, pageviews, rating, or any other stat the script tracks. It also does all this dynamically, so you only have to make one button.

To customize your Stat Buttons, you have to edit config_buttons.php. Here is how it originally looks:
1.  $result = $db->Execute("SELECT total_ratings, num_ratings, unq_pv_today, (unq_pv_today + unq_pv_1 + unq_pv_2 + unq_pv_3) / 4, tot_pv_today, (tot_pv_today + tot_pv_1 + tot_pv_2 + tot_pv_3) / 4, unq_in_today, (unq_in_today + unq_in_1 + unq_in_2 + unq_in_3) / 4, tot_in_today, (tot_in_today + tot_in_1 + tot_in_2 + tot_in_3) / 4, unq_out_today, (unq_out_today + unq_out_1 + unq_out_2 + unq_out_3) / 4, tot_out_today, (tot_out_today + tot_out_1 + tot_out_2 + tot_out_3) / 4
2.                          FROM ".$CONFIG['sql_prefix']."_members, ".$CONFIG['sql_prefix']."_stats
3.                          WHERE id = id2 AND id = ".$FORM['id']);
4.  list($total_ratings, $TMPL['ratenum'], $TMPL['unq_pv_tod'], $TMPL['unq_pv_avg'], $TMPL['tot_pv_tod'], $TMPL['tot_pv_avg'], $TMPL['unq_in_tod'], $TMPL['unq_in_avg'], $TMPL['tot_in_tod'], $TMPL['tot_in_avg'], $TMPL['unq_out_tod'], $TMPL['unq_out_avg'], $TMPL['tot_out_tod'], $TMPL['tot_out_avg']) = $db->FetchArray($result);
5.      $result = $db->SelectLimit("SELECT count(*) FROM ".$CONFIG['sql_prefix']."_stats WHERE ((unq_pv_today + unq_pv_1 + unq_pv_2 + unq_pv_3) / 4) >= ".$TMPL['unq_pv_avg'], $CONFIG['button_num'], 0);
6.      list($TMPL['rank']) = $db->FetchArray($result);
7.  $TMPL['avg_rate'] = $TMPL['ratenum'] ? round($total_ratings / $TMPL['ratenum'], 0) : 0;
8.  
9.  $img = imagecreatefrompng($CONFIG['button_dir'].'/ranking.png');
10. $color1 = imagecolorallocate($img, 0, 0, 255);
11. $color2 = imagecolorallocate($img, 255, 255, 255);
12. header ("Content-type: image/png");
13. imagestring($img, 2, 52, 0, $TMPL['unq_pv_tod'], $color1);
14. imagestring($img, 2, 60, 11, $TMPL['unq_pv_avg'], $color1);
15. imagestring($img, 2, 33, 24, $TMPL['avg_rate'].'/5', $color1);
16. imagestring($img, 5, 126, 20, $TMPL['rank'], $color2);
17. imagepng($img);
Most people should not change the first seven lines; they just get the data from the database.

Line 9 gets the base image you're using. The default is ranking.png, but it's ugly so you'll probably want to create your own button.

Lines 10 and 11 define colors that you're going to use in the image, based on Red, Green, and Blue values. The max value for any one is 255. In this case, $color1 is red and $color2 is black.

Line 12 just tells the browser that it will soon be recieving an image.

Lines 13 through 16 are where the magic happens. They use PHP's imagestring() function to print text to the base image. The first argument is $img, our image. The second is the font size. The third and fourth arguments are the X and Y coordinates of where the text should start, in pixels. The fifth argument is the text that you want printed. Look at line 4 to see all the different things you can put there. And finally, the last argument is the color you want. Depending on how complicated your Stat Buttons are, you can add or remove lines here.

The last line outputs our button to the browser.

If you have problems or questions, feel free to ask at the Support Forum.