Here is the code journal. Here I will post with a heading, what i've improved/fixed and will supply code. This makes a great place to reference back, without 20 documents riddled through the hard drive lol
-------
I got tired of looking at avail_slots() how it was so I made better, with the db class of course.
And the main function to check this. I also cleaned this up and tested on a naked page impersonating my secondary account for accuracy.
The secret here is making use of SQL properly and utilizing JOINS when needed. No need to call on 2 queries. I didn't bother doing a join-less query as that just complicates testing.
-------
Pager. Counting total torrent row results in a slim fashion. pager() picks up on count value to generate the 0,1 LIMIT. My JS now reads this variable, and old pager is removed indefinitely.
And we have [color=rgb(226, 80, 65)]$count[/color]
-------
Cleaning up Cleanup lol and adding better checks, removing the silly INT gmtime value and using DATETIME in place.
-------
Messages. As I've taken a try at a working type, its not built right. So redoing with a message class, using this query I found on stackoverflow
Threaded messages. Both IDs between both sender and receiver are there if query run for (receiver = 1) - 1 = admin (very first account)
So thats a good step for messages and gets all one needs.
-------
I got tired of looking at avail_slots() how it was so I made better, with the db class of course.
// Max Slots
function avail_slots($user_id, $user_class)
{
global $db;
$checkslots = $db->query_params('SELECT u.class, u.user_id, g.group_id, g.level, g.maxslots FROM groups g LEFT JOIN users u ON u.class = g.group_id WHERE g.group_id = :user_class', array('user_class' => $user_class));
$row = $checkslots->fetch();
if (array_key_exists('warned', $row)) { // does the column exist?
if ($row['warned'] == 'yes'): $maxslot = 1; else: $maxslot = $row['maxslots']; endif;
return $maxslot;
}
}
And the main function to check this. I also cleaned this up and tested on a naked page impersonating my secondary account for accuracy.
$maxslots = $db->query_params("SELECT COUNT(DISTINCT torrent_id) AS total FROM peers WHERE user_id = :user_id AND seeder = 'no'", array('user_id' => $user_row['user_id']));
foreach ($maxslots as $slots)
$totalslots = $slots['total'];
$maxslot = avail_slots($user_row['user_id'], $user_row['class']);
if ($totalslots >= $maxslot) fail("Maximum $totalslots/$maxslot download slots exceeded! Finish current transfers to reclaim slots.");
The secret here is making use of SQL properly and utilizing JOINS when needed. No need to call on 2 queries. I didn't bother doing a join-less query as that just complicates testing.
-------
Pager. Counting total torrent row results in a slim fashion. pager() picks up on count value to generate the 0,1 LIMIT. My JS now reads this variable, and old pager is removed indefinitely.
$countdb = $db->query_params('SELECT COUNT(torrent_id) FROM torrents');
foreach ($countdb->fetch() as $total) $count = $total;
And we have [color=rgb(226, 80, 65)]$count[/color]
-------
Cleaning up Cleanup lol and adding better checks, removing the silly INT gmtime value and using DATETIME in place.
function autoclean()
{
global $db, $CONFIG;
require_once '../cleanup.php';
$clean_time = $db->query_params("SELECT last_time FROM tasks WHERE task='cleanup'");
$clean = $clean_time->fetch();
$last_time = $clean['last_time'];
if (empty($clean)) $db->query_params("INSERT INTO tasks (task, last_time) VALUES ('cleanup', now())"); return;
if ($last_time + $CONFIG['autoclean_interval'] < date()) return;
$planned_clean = $db->query_params("UPDATE tasks SET last_time = now() WHERE task='cleanup' AND last_time = :last_time", array('now' => $last_time));
if (!empty($planned_clean)) return;
do_cleanup();
}
-------
Messages. As I've taken a try at a working type, its not built right. So redoing with a message class, using this query I found on stackoverflow
SELECT CONCAT(GREATEST(sender,receiver)," ",LEAST(sender,receiver)) AS thread, MAX(CONCAT(added,"|",msg)) as added, MAX(added) AS last_message, messages.* FROM messages WHERE receiver= 1 GROUP BY added ORDER BY added DESCThreaded messages. Both IDs between both sender and receiver are there if query run for (receiver = 1) - 1 = admin (very first account)
So thats a good step for messages and gets all one needs.