1
skspam
Calculation error in module
  • 2008/4/23 14:01

  • skspam

  • Just popping in

  • Posts: 16

  • Since: 2008/1/27


Module - Debaser

Using - Xoops, PHP/MySQL

MySQL setup for 'rating' is DOUBLE, 8,4, NOT NULL, default 0.

Calculating a music chart where users can rate songs from 1-10. Ratings are added together to create a total ratings chart. Once the total ratings reach 999 and another person rates the calculated total rating reverts to 1.


-Function to update ratings-


/* updates rating data in itemtable for a given item */ function updatedebaserrating($sel_id) { global $xoopsDB, $xoopsModuleConfig; $totalrating = 0; $votesDB = 0; $finalrating = 0; $query = " SELECT rating FROM ".$xoopsDB->prefix('debaservotedata')." WHERE lid = ".intval($sel_id)." "; $voteresult = $xoopsDB->query($query); $votesDB = $xoopsDB->getRowsNum( $voteresult ); while (list($rating) = $xoopsDB->fetchRow($voteresult)) { $totalrating += $rating; } if (($totalrating) != 0 && $votesDB != 0) { $finalrating = $totalrating / $votesDB; $finalrating = number_format($finalrating, 4); } $xoopsDB->queryF(" UPDATE " . $xoopsDB -> prefix('debaser_files') . " SET rating = '$finalrating', votes = '$votesDB' WHERE xfid = $sel_id" ); } /* -- */


-PHP to calculate ratings-

include 'header.php'; include_once XOOPS_ROOT_PATH.'/modules/debaser/include/functions.php'; foreach ( $_POST as $k => $v ) { ${$k} = $v; } foreach ( $_GET as $k => $v ) { ${$k} = $v; } if (empty($_POST['submit'])) { $_POST['submit'] = ''; } include XOOPS_ROOT_PATH . '/header.php'; if ( $_POST['submit'] ) { $ratinguser = (is_object($xoopsUser)) ? $xoopsUser->uid() : 0; $rating = ($_POST['rating']) ? $_POST['rating'] : 0; // Make sure only 1 anonymous from an IP in a single day. $anonwaitdays = 1; $ip = getenv( "REMOTE_ADDR" ); $lid = intval($_POST['lid']); // Check if Rating is Null if (!$rating) { redirect_header("singlefile.php?id=$lid", 1, _MD_DEBASER_NORATING); exit(); } // Check if REG user is trying to vote twice. if ($ratinguser >= 1) { $yesterday = (time() - (86400 * $anonwaitdays)); $result = $xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix('debaservotedata')." WHERE lid = $lid AND ratinguser >= 1 AND ratinghostname = '$ip' AND ratingtimestamp > $yesterday"); list($anonvotecount) = $xoopsDB->fetchRow($result); if ($anonvotecount >= 1) { redirect_header("singlefile.php?id=$lid", 1, _MD_DEBASER_VOTEONCE); exit(); } } // Check if ANONYMOUS user is trying to vote more than once per day. if ($ratinguser == 0) { $yesterday = (time() - (86400 * $anonwaitdays)); $result = $xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix('debaservotedata')." WHERE lid = $lid AND ratinguser = 0 AND ratinghostname = '$ip' AND ratingtimestamp > $yesterday"); list($anonvotecount) = $xoopsDB->fetchRow($result); if ($anonvotecount >= 1) { redirect_header("singlefile.php?id=$lid", 1, _MD_DEBASER_VOTEONCE); exit(); } } // All is well. Add to Line Item Rate to DB. $newid = $xoopsDB->genId($xoopsDB->prefix('debaservotedata'). "_ratingid_seq"); $datetime = time(); $xoopsDB->query("INSERT INTO ".$xoopsDB->prefix('debaservotedata')." (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp) VALUES ($newid, $lid, $ratinguser, $rating, '$ip', $datetime)" ); // All is well. Calculate Score & Add to Summary (for quick retrieval & sorting) to DB. updatedebaserrating($lid); $ratemessage = _MD_DEBASER_VOTEAPPRE . "
" . sprintf(_MD_DEBASER_THANKYOU, $xoopsConfig['sitename']); redirect_header("singlefile.php?id=$lid", 1, $ratemessage); exit(); } else { redirect_header("singlefile.php?id=$lid", 1, _MD_DEBASER_UNKNOWNERROR); exit(); } include 'footer.php';?>


I have been searching for a couple days now but can't find the problem. I need the calculations to continue up to 10000. Number of Votes is calculating right. The only thing that is reverting back to 1 is the rating calculations when they go above 999.

Can anyone help?

Thanks
Sandy

2
solo71
Re: Calculation error in module
  • 2008/4/23 18:08

  • solo71

  • Module Developer

  • Posts: 941

  • Since: 2003/1/29


LOL!

Do you really think we are processors..?

How do you want us to read that?


3
Runeher
Re: Calculation error in module
  • 2008/4/23 18:25

  • Runeher

  • Module Developer

  • Posts: 825

  • Since: 2008/1/24


hehehe...

4
skspam
Re: Calculation error in module
  • 2008/4/23 18:29

  • skspam

  • Just popping in

  • Posts: 16

  • Since: 2008/1/27


Thank you so much for the kind words Sir. When I copied and pasted it appeared to have all of the page returns intact. My error, sorry I wasted your time. No, I do not think you are processors. You are not very helpful either. You are a co-webmaster? (And -- If anyone knows how to delete my previous post I would appriciate it. If this gentlemans comment is any indication of the kind of help you offer, then I will go it alone. Have a wonderful day!

5
Catzwolf
Re: Calculation error in module
  • 2008/4/23 18:41

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


Hiya Sandy,

I believe that your issue could be the 'value' length of the rating field in that table. The reason is that it reverting back to 1 would indicate that the field has hit its full value, and the database doesn't know what to do with it, except revert back to the start again.

6
skspam
Re: Calculation error in module
  • 2008/4/23 18:50

  • skspam

  • Just popping in

  • Posts: 16

  • Since: 2008/1/27


Thanks for your assistance. I will check that.

7
solo71
Re: Calculation error in module
  • 2008/4/25 15:52

  • solo71

  • Module Developer

  • Posts: 941

  • Since: 2003/1/29


Sorry for my rudeness. I'm not a co-web on this website actually.

Please, note this was just a way to say : take care of the way you ask assistance. Generally speaking a good question generally leads to an effective help...

Fortunatly our friend Catz found the way through the fog.


Login

Who's Online

320 user(s) are online (270 user(s) are browsing Support Forums)


Members: 0


Guests: 320


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