Monk3h Posted April 24, 2008 Share Posted April 24, 2008 I am currently using the Delete from Command to clear chat on my game. Is there a way for me to Log all the data into a text file and then delete it all from the Chat Table in my Database thru PHP? I mean asin a tool i can make to do it for me from my Admin Options in game so other Admins can do the same? Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/ Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 this is an example: <?php $chat_user = array(); $chat_data = array(); $sql = "SELECT * FROM chat;"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)){ $chat_user[] .= $row['user']; $chat_log[] .= $row['log'] } $count = 0; $file_data = ""; foreach ($chat_user as $value){ $file_data .= $value.":".$chat_log[$count]."\n"; $count++; } $file = "log_".date("Y-m-D").".txt"; $directory ="path/to/saves/"; //must be writeable $handle = fopen ($directory.$file, "w+"); fwrite ($handle, $file_data); fclose($handle); ?> Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525607 Share on other sites More sharing options...
Monk3h Posted April 24, 2008 Author Share Posted April 24, 2008 I dont understand that.. :/ This is my current Script to Clear chat. Could you add it to to that so i can see what does what the the Values im using? Or explain what each party does? Im not just trying to get you to do my coding for me i Just want to understand what does what. if ($view == clearc) { mysql_query("delete from chat"); mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')"); print "You cleared the chat."; } Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525616 Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 <?php if ($view == clearc) { $chat_user = array(); $chat_data = array(); $sql = "SELECT * FROM chat;"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)){ $chat_user[] .= $row['user']; $chat_log[] .= $row['chat']; } $count = 0; $file_data = ""; foreach ($chat_user as $value){ $file_data .= $value.":".$chat_log[$count]."\n"; $count++; } $file = "log_".date("Y-m-D").".txt"; //automatically genrates a new file named something like //log_2008-01-12.txt $directory ="path/to/saves/"; //must be writeable change it to where you want the logs to be saved. $handle = fopen ($directory.$file, "w+"); fwrite ($handle, $file_data); fclose($handle); mysql_query("delete from chat"); mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')"); print "You cleared the chat."; } ?> the script will get all the info in your db, and dump it into a file, then it will clear the db and post what you normally post. Simple as that Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525620 Share on other sites More sharing options...
Monk3h Posted April 24, 2008 Author Share Posted April 24, 2008 Warning: fopen(path/to/saves/log_2008-04-Wed.txt) [function.fopen]: failed to open stream: No such file or directory in /home/monk3h/public_html/admin.php on line 180 Warning: fwrite(): supplied argument is not a valid stream resource in /home/monk3h/public_html/admin.php on line 181 Warning: fclose(): supplied argument is not a valid stream resource in /home/monk3h/public_html/admin.php on line 182 You cleared the chat. o.O Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525626 Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 did you read my comments? make sure it's writeable. if it isn't, that will happen. change the "path/to/saves" to the PATH TO SAVES that you want to use. Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525627 Share on other sites More sharing options...
Monk3h Posted April 24, 2008 Author Share Posted April 24, 2008 Oops, done. But that just posts the Chat cleared by Admin Part. Not what was in the Chat table before the Clear. Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525630 Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 what is cleared is cleared. the script is linear, meaning that step1->step2->step3-> I have it written so that it does this: first, it gets all the data, and dumps it in 2 arrays, then it outputs that data into a variable, then writes that variable into a file, then clears the table. If it's writing only that data, it's because that's the only thing there. Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525631 Share on other sites More sharing options...
Monk3h Posted April 24, 2008 Author Share Posted April 24, 2008 Even after spamming my Chat it still just Logs the same thing. Are you sure its not logging after it deletes? I understand what your saying but there is no reason for it to log just 1 line of chat, especialy just the first line. :S Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525632 Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 this should fix that issue (if not, let me know) <?php if ($view == clearc) { $chat_user = array(); $chat_data = array(); $sql = "SELECT * FROM chat;"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)){ $chat_user[] = $row['user']; $chat_log[] = $row['chat']; } $count = 0; $file_data = ""; foreach ($chat_user as $value){ $file_data .= $value.":".$chat_log[$count]."\n"; $count++; } $file = "log_".date("Y-m-d").".txt"; //automatically genrates a new file named something like //log_2008-01-12.txt $directory ="path/to/saves/"; //must be writeable change it to where you want the logs to be saved. $handle = fopen ($directory.$file, "w+"); fwrite ($handle, $file_data); fclose($handle); mysql_query("delete from chat"); mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')"); print "You cleared the chat.<br />"; print "DATA IN NEW FILE:\n".$file_data."<br />"; print "data stored in <a href='".$directory.$file."'>".$directory.$file."</a>"; } ?> Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525633 Share on other sites More sharing options...
Monk3h Posted April 24, 2008 Author Share Posted April 24, 2008 This time it outputted after the Chat cleared text everything it should have logged. Which is cool. But it still didnt Log what was in chat. :S Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525634 Share on other sites More sharing options...
Monk3h Posted April 24, 2008 Author Share Posted April 24, 2008 Also, how do i make it so it dosnt overwright the last Log? So every time i Clear chat it saves as a seperate file. Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525639 Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 <?php if ($view == clearc) { $chat_user = array(); $chat_data = array(); $sql = "SELECT * FROM chat;"; $result = mysql_query($sql); while ($row = mysql_fetch_assoc($result)){ $chat_user[] = $row['user']; $chat_log[] = $row['chat']; } $count = 0; $file_data = ""; foreach ($chat_user as $value){ $file_data .= $value.":".$chat_log[$count]."\n"; $count++; } $file = "log_".date("Y-m-d_Gi_s").".txt"; //automatically genrates a new file named something like //log_2008-01-12.txt $directory ="path/to/saves/"; //must be writeable change it to where you want the logs to be saved. $handle = fopen ($directory.$file, "w+"); fwrite ($handle, $file_data); fclose($handle); mysql_query("delete from chat"); mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')"); print "You cleared the chat.<br />"; print "DATA IN NEW FILE:\n".$file_data."<br />"; print "data stored in <a href='".$directory.$file."'>".$directory.$file."</a>"; } ?> Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525648 Share on other sites More sharing options...
Monk3h Posted April 24, 2008 Author Share Posted April 24, 2008 Thankyou so much! Works now! XD Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525653 Share on other sites More sharing options...
jonsjava Posted April 24, 2008 Share Posted April 24, 2008 please mark this thread resolved. thanks! Link to comment https://forums.phpfreaks.com/topic/102628-solved-logging-data-into-a-text-file-from-a-database/#findComment-525658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.