1
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