PDA

View Full Version : Find the bug in my PHP script :P


bluemicrobyte
08-04-2006, 7:33 PM
Alright so I have this PHP script that isn't working the way it should be.

This is the debug output: http://www.bluemicrobyte.com/scripts/cron/levelprogression.php

Notice that no matter how many times you refresh the page, the data doesnt change. It SHOULD increase the points each time the page is refreshed, and level up users accordingly. Here's the PHP script:

Note that the script worked perfectly starting from an empty table until it hit the values its at now and froze. I originally had the $newpoints set to 24, but the values all froze at 24. Then I changed $newpoints to 67, then it worked until they hit 67 and froze.

/*
This script, when called even just once, will calculate the number of points each user has earned in the past 24 hours. It updates the phpbb_ina_levels table. Notes: the points are points they have. Points to next level is not stored and is dynamically calculated, something like current level times 10 should give us a good number.
*/

// Set the clock to be GTM - 10
putenv('TZ=Pacific/Honolulu');

require '/home/blue/public_html/forums/db/mysql.php';

// First connect to the database
require '/home/blue/public_html/forums/config.php';
// Make the database connection.
$db = new sql_db($dbhost, $dbuser, $dbpasswd, $dbname, false);
if(!$db->db_connect_id)
{
message_die(CRITICAL_ERROR, "Could not connect to the database");
}
unset($dbpasswd);

//
// Figure out who's been on in the past 24 hours
$dayago = time() - (60 * 60 * 24); // seconds * hours * 24hours/1day

$query = "SELECT * FROM phpbb_users WHERE user_lastvisit > $dayago";
$result = $db->sql_query($query) or die ('could not query'.mysql_error());
$user_ids = array();
while ($row = $db->sql_fetchrow($result)) {
array_push($user_ids, $row['user_id']);
}

//
// Big huge loop, done once for each user that logged in in the past 24 hours
foreach($user_ids as $user_id) {

//
// Lets start gathering points!
$newpoints = 67; // temporary value


//
// Add the new points, and increase level

// If they aren't in the table, create an entry
$query = "SELECT * FROM phpbb_ina_level WHERE user_id = '".$user_id."'";
$result = $db->sql_query($query) or die ('could not query'.mysql_error());
if (mysql_num_rows($result) == 0) {
$query = "INSERT INTO phpbb_ina_level (user_id, level, percent, points) VALUES ('$user_id', 1, 0, 0)";
$db->sql_query($query) or die ('could not query'.mysql_error());
}

// Dump their current standings into a data array
$tempdata = mysql_fetch_row($result);
$olddata['user_id'] = $tempdata[0];
$olddata['user_level'] = $tempdata[1];
$olddata['user_percent'] = $tempdata[2];
$olddata['user_points'] = $tempdata[3];

// Now correctly calculate and update their level, percent, points
// NOTE: a users needs to earn (current level * 100) points to level up.
$newdata['user_id'] = $olddata['user_id']; // no change here.... ever

$points_to_level = $olddata['user_level'] * 100;
$current_points = $olddata['user_points'] + $newpoints;

// Have they leveled up?
if ($current_points >= $points_to_level) {
$newdata['user_level'] = $olddata['user_level'] + 1;
$current_points = $current_points - $points_to_level; // update points, since we leveled them
} else {
$newdata['user_level'] = $olddata['user_level'];
$points_to_level = $newdata['user_level'] * 100; // update this
}
$newdata['user_points'] = $current_points;
$newdata['user_percent'] = round(100 * ($newdata['user_points'] / $points_to_level));

// DEBUG
echo 'current points: '.$current_points.'<br>';
echo 'points to level: '.$points_to_level.'<br>';
echo 'olddata array: ';
print_r($olddata);
echo '<br> newdata array; ';
print_r($newdata);
echo '<br><br>';

//
// Finally, save the new data to the database
$query = "UPDATE phpbb_ina_level SET
level = '".$newdata['user_level']."',
percent = '".$newdata['user_percent']."',
points = '".$newdata['user_percent']."'
WHERE user_id = '".$newdata['user_id']."'";
$db->sql_query($query) or die ('could not query'.mysql_error());

} // Now go back and do it for all the other users! (aka close loop)

Jeff
08-05-2006, 1:23 AM
// Finally, save the new data to the database
$query = "UPDATE phpbb_ina_level SET
level = '".$newdata['user_level']."',
percent = '".$newdata['user_percent']."',
points = '".$newdata['user_percent']."'
WHERE user_id = '".$newdata['user_id']."'";
$db->sql_query($query) or die ('could not query'.mysql_error());


Points is being assigned the percent.

bluemicrobyte
08-05-2006, 4:24 AM
Aha! Brilliant! Thanks, Jeff! Only someone who has never seen the script before and hasn't spent the past hour staring at it could have spotted that simple mistake =P

Black.Ice
08-05-2006, 12:46 PM
That is so true, bluemicrobyte. I've found it when you encounter a problem like this, work on the next step of your project -- clear it from your head. Then eventually, return back to it and you'll most likely be able to catch simple mistakes like that. I've done it a countless number of times.