Statistics = Raise?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Statistics = Raise?

Post by SilverMist »

Hello all! I am looking for a script that, like when you pay your bills (on a virtual reality/SIM game) your general "Home Maintenance" stats would raise by one point. A database can be used, and code snippets would be lovely! Thanks in advance!
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Code: Select all

if($billspaid){
    $sql = "UPDATE tablename SET stats=stats+1 WHERE username = '$name' LIMIT 1";
    $query=mysql_query($sql) or die(mysql_error());
}
And somehow set a variable if the bills are paid.
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Post by SilverMist »

Okay, well I get that, but how will I make the little image stat bar thingy show up? Thanks!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Steveo31 wrote: ... WHERE username = '$name' LIMIT 1 ...
Heh, one more paranoid here :lol:
No offence, I'm the one too =)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

SilverMist wrote:Okay, well I get that, but how will I make the little image stat bar thingy show up? Thanks!
Can you explain?
User avatar
SilverMist
Forum Commoner
Posts: 65
Joined: Tue Mar 02, 2004 2:06 pm
Location: Canada
Contact:

Post by SilverMist »

There will be a small red bar (picture) which shows your statistics. Right after the bar will be say 57% Maintained. The red bar would show 57% of 100. Is that a little bit clearer? Sorry for being so vague!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

You can either have several pictures already made - called like 0.jpg, 5.jpg, 10.jpg etc and then call the one that is closest to the % you want to show.

The second option is to use GD to make an image on the fly that represents the % level.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

In the query, play around with SUM(), AVG() and other math functions to get the result ( in this case, 57% out of 100% ) you desire.
http://dev.mysql.com/doc/mysql/en/Mathe ... tions.html

To show the images, some ideas I can think of is to use <img src="foo.jpg" height="57%" /> or <img src="foo.jpg" height="57px" /> depending on flavour of choise or use the 57% value in any other way; <div>,<td>,<table> height/width.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

SilverMist wrote:Okay, well I get that, but how will I make the little image stat bar thingy show up? Thanks!
you arw probably looking at GD functionality to be able to display image of that type (dynamic) GD is quite a complicated subject i sugest you read up0 on it
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

This is pretty simple.

First, you don't want GD to dynamically generate the static images. That's too much, and way overboard. Rather, let's say you want to use something like a 5 star image. If you have a rating of, let's say, 10, you have a half start, 20 = full star, 40 = 2 full stars, 60 = 3 full stars, etc.

So, now you name your star images 1.jpg, 2.jpg, 3.jpg, 4.jpg, etc. 1.jpg would be for people with at least 10 points, 2.jpg for at least 20 points, etc. 10.jpg for people with 100 points (and therefore, 5 stars). 0.jpg for people with less than 10 points, and therefore, 0 stars.

So anyways, after all this setup (which really isn't much), you can do something like this:

Code: Select all

if ( strlen($rating) == 3 ) {
	$img = '10';
} elseif ( strlen($rating) == 1 ) {
	$img = '0';
} else {
	$img = $rating{0};
}
echo '<img src="path/to/images'.$img.'.jpg">';
So you could use something like that. Pretty simple, I think.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

just as a example, incase someone else wants to use it in a similar problem, but jason is right lol.

image.php

Code: Select all

<?php
header ("Content-type: image/png");
$l = $_GET['rate'] * 2 + 1;
if(isset($_GET['limit']) AND $l > $_GET['limit'])
{
$l = $_GET['limit'];
}
$img_handle = ImageCreate ($l, 12) or die ("Cannot Create image");
$back_color = ImageColorAllocate($img_handle, 200, 0, 0); 
if(isset($_GET['text']))
{
$text_len = strlen($_GET['text']) * 7;
$txt_color = ImageColorAllocate ($img_handle, 255, 255, 255);
imagestring($img_handle, 3, $l - $text_len, 0, $_GET['text'], $txt_color);
}
ImagePng ($img_handle); 
?>
how to use it

Code: Select all

&lt;img src="image.php?rate=50&amp;limit=300" /&gt;
"limit" is used to stop the image getting to big, in other words, this is the max width of the bar, even if the "rate" is 5000.

show text on bar as well?

Code: Select all

&lt;img src="image.php?rate=50&amp;limit=300&amp;text=50%" /&gt;
play around with it, change colors or whatever you need.
Post Reply