Re: Riderz 'how to create last activities list'

Started on the old forum in June 2010 · view the original in the archive · old links redirect here

5 replies · 560 views · started 16 years ago · 1 min read

3 participants
CreativityKills Lead · 833 posts #1

For some strange reason the mobile version of this site doesnt allow me to send messages. If i got ur msg rite, it was about creating the most recent activities. I assume u already knw ur database enuf to make a table 4 it.

$sql = "SELECT * FROM user_activities ORDER BY act_time DESC LIMIT 5";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)
{
while($results = mysql_fetch_array($query)){
$user = getnick_uid($results['user_id']);
echo "{$user} {$results['action']} @ {$results['act_time']}<br/>";
}
}else{
echo 'wtf! No activities? Boring site!';
}

forgive me if i didnt get sum mysql functions ryt. I use PHP Doctrine for managing databases in all my projects now, im already used to creating models and yaml schemas sorry bt u get the idea though.

CreativityKills Lead · 833 posts #4

A brilliant idea would be, create a class that helps manage the activity functions

<?php defined('__CK__') OR die('Silence is golden.');
class Activity_Log {
public $auto_clear = TRUE;
protected $expiry_time = 86000;

public function __construct(){
 if($this->auto_clear){
 /// auto_clear old activities
 $this->clear_old($this->expiry_time);
 }
}

public function clear_old($expiry_time = 86000){
 /// check for old acts
 $time_limit = time() - $expiry_time;
 $num_rows = mysql_num_rows(mysql_query("SELECT * FROM user_activities WHERE act_time < {$time_limit}"));
 if($num_rows > 0){
  mysql_query("DELETE FROM user_activities WHERE act_time < {$time_limit}");
 }
}

public function add_activity($activity,$uid = NULL){
$updated = FALSE;
 /// Update MANY activities at once
 if(is_array($activity) AND count($activity) > 0){
 /// Update each record
  foreach($activity as $act){
  $updated = mysql_query("INSERT INTO user_activities SET action = '".$act['msg']."', uid = '".(int) $act['uid']."', act_time = '".(int) $act['time']."'");
 }
 }else{
 /// Update single record
  $updated = mysql_query("INSERT INTO user_activities SET action = '".$activity."', uid = '".(int) $uid."', act_time = '".time()."'");
 }
return ($updated) ? TRUE : FALSE;
}

public function delete_activity($id){
$deleted = mysql_query("DELETE FROM user_activity WHERE id = '".(int) $id."'");
return ($deleted) ? TRUE : FALSE;
}
}
?>
CreativityKills Lead · 833 posts #5

You can add other methods to the class. Quick explanation:

  1. Instantiate the class
include('../path/to/class_file.php');
define('__CK__', TRUE);
$activity = new Activity_Logger;
/// NOTE: It automagically clears old activities
  1. Adding activities for a user. 2.1 Single activity.
/// Assuming class has been instantiated
$act = 'just joined mGiGS.';
$activity->add_activity($act,$uid);

2.2 Multiple activities at once.

$acts = array(
 [0] => array('msg' => 'just joined mGiGS.', 'uid' => $uid, 'time' => time()),
 [1] => array('msg' => 'also updated his profile', 'uid' => $uid, 'time' => time())
);

$activity->add_activity($acts);
  1. Deleting a record
$delornot = ($activity->delete_activity($activity_id)) ? 'was deleted successfully.' : 'record #'.$activity_id.' could not be deleted.';
echo 'Activity '.$delornot;
centi Developer · 159 posts #6

rider dude pm me.. i was busy last few days. or send me a mail ill fwrd the full code

😳 .................................... http://photomag.lk/ .................................... 🤨

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close