Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][JavaScript][MSSQL][AJAX] Shoutbox
DaSki
post 27.07.2008, 17:55:10
Post #1





Grupa: Zarejestrowani
Postów: 21
Pomógł: 0
Dołączył: 19.01.2008

Ostrzeżenie: (0%)
-----


Witam mam problem z shoutbox'em.
Pobrałem shoutbox z tej strony http://www.ajaxdaddy.com/demo-jquery-shoutbox.html.
Dobrze działa ale ciągle próbuje go przerobić na MySQL bo on zapuisuje wszystkie wiadomości do pliku.
Udało mi się zrobić żeby zapisywał do MySQL, ale teraz nie wiem jak zrobić by pokazał mi rekordy z bazy.
Tu jest plik który zapisuje i odczytuje z pliku te wiadomości z shoutboxa:

  1. <?php
  2. function replace(&$item, $key) {
  3. $item = str_replace('|', '-', $item);
  4. }
  5.  
  6. if (!function_exists('file_put_contents')) {
  7. function file_put_contents($fileName, $data) {
  8. if (is_array($data)) {
  9. $data = join('', $data);
  10. }
  11. $res = @fopen($fileName, 'w+b');
  12. if ($res) {
  13. $write = @fwrite($res, $data);
  14. if($write === false) {
  15. return false;
  16. } else {
  17. return $write;
  18. }
  19. }
  20. }
  21. }
  22.  
  23. //file_put_contents('debug.txt', print_r($_GET, true));
  24. switch($_GET['action']) {
  25. case 'add':
  26. array_walk($_POST, 'replace');
  27. $arr = file('messages.txt');
  28.  
  29. if(count($arr) > 10)
  30. array_shift($arr);
  31.  
  32. $_POST['nickname'] = htmlentities($_POST['nickname']);
  33. $_POST['message'] = htmlentities($_POST['message']);
  34. $time = time();
  35. $arr[] = $time.'|'.$_POST['nickname'].'|'.$_POST['message'].'|'.$_SERVER['REMOTE_ADDR']."\n";
  36. file_put_contents('messages.txt', implode('', $arr));
  37.  
  38. $data['response'] = 'Good work';
  39. $data['nickname'] = $_POST['nickname'];
  40. $data['message'] = $_POST['message'];
  41. $data['time'] = $time;
  42. break;
  43.  
  44. case 'view':
  45. $data = array();
  46. $arr = file('messages.txt');
  47. if(!$_GET['time'])
  48. $_GET['time'] = 0;
  49. foreach($arr as $row) {
  50. $aTemp = null;
  51. list($aTemp['time'], $aTemp['nickname'], $aTemp['message']) = explode('|', $row); 
  52. if($aTemp['message'] AND $aTemp['time'] > $_GET['time'])
  53. $data[] = $aTemp;
  54. }
  55. //file_put_contents('debug.txt', print_r($data, true));
  56. break;
  57. }
  58.  
  59. require_once('JSON.php');
  60. $json = new Services_JSON();
  61. $out = $json->encode($data);
  62. print $out;
  63. ?>
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 15.06.2025 - 10:48