1
deka87
Need help on a custom php script
  • 2009/2/17 18:44

  • deka87

  • Friend of XOOPS

  • Posts: 1125

  • Since: 2007/10/5


hi, please help me out with a little script. i have a variable in the registration form which is
$rate
that is numeric. I need a script to calculate the average
$rate
of all the users who've submitted it. Like to sum up all the $rate values from the users and devide it by the number of the users who's submitted it. help me out with this one guys.

regards,

Yevgeny
Mind anchors

2
deka87
Re: Need help on a custom php script
  • 2009/2/17 20:38

  • deka87

  • Friend of XOOPS

  • Posts: 1125

  • Since: 2007/10/5


xoops 2.2
Mind anchors

3
ghia
Re: Need help on a custom php script
  • 2009/2/18 1:19

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Do in your script an SQL query with
SELECT AVGrate ) AS avgCOUNTrate ) AS cnt
 FROM xoops_user_profile
 WHERE rate IS NOT NULL

Query result field avg is your average of the rate and cnt is the number of users that filled in the value, assuming the field is empty when it is not filled in, else use
WHERE rate <> 0

4
deka87
Re: Need help on a custom php script
  • 2009/2/18 6:19

  • deka87

  • Friend of XOOPS

  • Posts: 1125

  • Since: 2007/10/5


thanks ghia, but im totally lost. am I supposed to get something like this:

global $xoopsDB;
$results 'SELECT AVG( min_rate ) AS avg, COUNT( min_rate ) AS cnt FROM ' $xoopsDB->prefix('xoops_user_profile') . ' WHERE min_rate IS NOT NULL';
while (
$avg_rate $xoopsDB->fetchArray($result)) { 
    
$avg $row['1']; 
    
$cnt $row['2']; 
}
echo 
'$avg/$cnt';


does it make any sense? please correct if not
Mind anchors

5
trabis
Re: Need help on a custom php script
  • 2009/2/18 13:51

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Maybe this:

global $xoopsDB;
$sql 'SELECT AVG( min_rate ) AS avg, COUNT( min_rate ) AS cnt FROM ' $xoopsDB->prefix('xoops_user_profile') . ' WHERE min_rate IS NOT NULL';
$result $xoopsDB->query($sql);
if(!
$result) {
    
$avg $cnt 0;
    echo 
"No result";
} else {
    list(
$avg$cnt) = $xoopsDB->fetchRow($result);
    echo 
"average=" $avg;
    echo 
"voters=" $cnt;
}



6
deka87
Re: Need help on a custom php script
  • 2009/2/18 14:44

  • deka87

  • Friend of XOOPS

  • Posts: 1125

  • Since: 2007/10/5


thanks trab
Mind anchors

7
ghia
Re: Need help on a custom php script
  • 2009/2/18 16:32

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Beware:
$xoopsDB->prefix('user_profile')

8
deka87
Re: Need help on a custom php script
  • 2009/8/19 3:37

  • deka87

  • Friend of XOOPS

  • Posts: 1125

  • Since: 2007/10/5


Quote:
global $xoopsDB;
$sql = 'SELECT AVG( min_rate ) AS avg, COUNT( min_rate ) AS cnt FROM ' . $xoopsDB->prefix('xoops_user_profile') . ' WHERE min_rate IS NOT NULL';
$result = $xoopsDB->query($sql);
if(!$result) {
$avg = $cnt = 0;
echo "No result";
} else {
list($avg, $cnt) = $xoopsDB->fetchRow($result);
echo "average=" . $avg;
echo "
voters=" . $cnt;
}


what is this supposed to be like for XOOPS 2.3.3? thanks

9
ghia
Re: Need help on a custom php script
  • 2009/8/19 7:27

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Use table profile_profile .

10
deka87
Re: Need help on a custom php script
  • 2009/8/19 12:10

  • deka87

  • Friend of XOOPS

  • Posts: 1125

  • Since: 2007/10/5


thanks ghia now it works. but the problem is it does count all the users to devide altho' i need only those who's got the $min_rate submitted. for ex. i got this: Quote:
average=0.00048739495798319
voters=2975
tho' $min_rate can vary from 0.01 up to 0.50 so the calculation doesnt make sense. any ideas?

also how do I limit the number of digits i get?

Login

Who's Online

303 user(s) are online (198 user(s) are browsing Support Forums)


Members: 0


Guests: 303


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits