Code Using a Database
Posted: Wed Feb 23, 2005 5:48 pm
Hey guys, this code will help you manage your guild data in a MySQL database. I do need some help in reading the "guildinfo.csv" file and putting the information into a table on the database. We also need to write this so it updates current members and adds new members as it reads. Here's what I have so far.
Code: Select all
<?php
include('db_connect.php');
global $guild_id;
global $local_timestamp;
global $remote_timestamp;
//////////////////////////////////////////////////////////////////
// SQL TABLE SETUP //
// Census = id, date //
// guild_roster = id, name, race, class, rank, level, last_seen //
//////////////////////////////////////////////////////////////////
// Resets the local census status field to null - Debuging Only
function ResetLocal()
{ $q = 'UPDATE `Census` SET `date` = "" WHERE `id` = 1 LIMIT 1';
$result = mysql_query($q);
if (!$result)
{ echo 'ERROR: Could not run query: '.mysql_error();
exit;
}
}
// Updates the local status field to variable $date
function UpdateLocal($date)
{ $q = 'UPDATE `Census` SET `date` = "'.$date.'" WHERE `id` = 1 LIMIT 1';
$result = mysql_query($q);
if (!$result)
{ echo 'ERROR: Could not run query: '.mysql_error();
exit;
}
echo'<br>Local TimeStamp Updated';
}
// Reads remote guildinfo.csv file and exports information to database
function GetRemoteData()
{ echo'<br>Getting Data From Census Server';
// NEED HELP WITH THIS CODE
// NEED HELP WITH THIS CODE
}
// Creates a table with guild information from local database
function GetLocalData()
{ echo'<br>Getting Data From Local Database';
$q = 'SELECT `name`,`race`,`class`,`rank`,`level`,`last_seen` FROM `guild_roster` ORDER BY `level` ASC ';
$result = mysql_query($q);
if (!$result)
{ echo 'ERROR: Could not run query: '.mysql_error();
exit;
}
echo'<hr>';
echo'<center>';
echo'<h1>Invictus Mortem</h1>';
echo'<table border=1>';
echo'<tr><td>Name</td><td>Race</td><td>Class</td><td>Rank</td><td>Level</td><td>Last Seen</td></tr>';
echo'<tr>';
$rows = mysql_num_fields($result);
while ($info = mysql_fetch_row($result))
{ for($i=0; $i<$rows-1;$i++)
{ echo'<td>'.$info[$i].'</td>';
}
echo'<td>'.strftime("%m/%d/%y %H:%M:%S",$info[$rows]).'</td>';
echo'</tr>';
}
echo'</table>';
echo'</center>';
}
// Reads Local Date
function GetLocalDate()
{ global $local_timestamp;
$q = "SELECT * FROM Census WHERE id=1";
$result = mysql_query($q);
if (!$result)
{ echo 'ERROR: Could not run query: '.mysql_error();
exit;
}
$info = mysql_fetch_row($result);
$local_timestamp = $info[1];
if($info[1]==null)
{ echo'First Time Running';
$q = 'UPDATE `Census` SET `date` = "0000000000" WHERE `id` = 1 LIMIT 1';
$result = mysql_query($q);
if (!$result)
{ echo 'ERROR: Could not run query: '.mysql_error();
exit;
}
$local_timestamp = "00000000";
echo '<br>Rest Timestamp';
echo '<br>Local Time Stamp : ' . strftime("%m/%d/%y %H:%M:%S",$local_timestamp) . '<br>';
}
else
{ echo 'TimeStamp: '.$info[1];
echo '<br>Local Time Stamp : ' . strftime("%m/%d/%y %H:%M:%S",$local_timestamp) . '<br>';
}
}
// Reads Remote Date
function GetRemoteDate()
{ global $remote_timestamp;
$filename = "http://www.warcraftrealms.com/exports/status.txt";
$infile = fopen ($filename, "r");
if (!$infile)
{ echo "<p>REMOTE: Unable to open status file.<br>";
exit;
}
if(!feof ($infile))
{ $buffer = fgets($infile, 4096);
$remote_timestamp = trim( $buffer );
echo 'Remote Time Stamp : ' . strftime("%m/%d/%y %H:%M:%S",$remote_timestamp) . '<br>';
}
fclose( $infile );
}
// Checks Time Stamps and Determines what to do
function CheckTimeStamps()
{ global $local_timestamp;
global $remote_timestamp;
echo'<br>';
echo'Checking Time Stamps....';
echo'<br>Local: '.$local_timestamp;
echo'<br>Reomote: '.$remote_timestamp;
$time = $remote_timestamp - $local_timestamp;
echo'<br>Difference in time strings: '.$time;
if( $time > 43200)
{ echo'<br>Update Needed';
UpdateLocal($remote_timestamp);
GetRemoteData();
GetLocalData();
}
else
{ echo'<br>Update Not Needed';
GetLocalData();
}
}
//ResetLocal(); // Uncomment for Debugging Only
GetLocalDate();
GetRemoteDate();
CheckTimeStamps();
?>