2011-11-04 1 views
0

현재 몇 가지 문제가 있습니다. 나는 채팅 기능이있는 웹 사이트를 만들고 있습니다. 현재로서는 사용자가 메시지를 제출할 때 메시지가 표시되는 영역을 업데이트 할 수있었습니다. 그러나 우리는 몇 가지 문제가 있습니다. 우리는 jquery를 사용하여 전체 페이지가 다시로드되지 않고 대신 iframe 만 알리도록했습니다. 채팅이 제대로 작동하고 몇 분 후에 다시로드되기 시작합니다. 여기에 우리가 지금 가지고있는 것이 있습니다 ...데이터베이스 업데이트시 웹 페이지 자동 업데이트

<?php 
    session_start(); 
    include "connect.php"; 
    $room = $_SESSION['room']; 
    $getnummessages="SELECT COUNT(*) as messagecount from tbl_chatmessages"; 
    $getnummessages2=mysql_query($getnummessages) or die("blah"); 
    $getnummessages3=mysql_result($getnummessages2, 0); 

    if($getnummessages3>21) 
    { 
     $startrow=$getmessages3-20; 
    } 
    else 
    { 
     $startrow=1; 
    } 

    date_default_timezone_set ("Asia/Manila"); 
    $date = date("Y-m-d"); 

    // Configuration part 
    $path = "images"; // Path to the directory where the emoticons are 


    //smiley 
    // Query the database, and assign the result-set to $result 
    $query = "SELECT emote, image FROM emoticons"; 
    $result = mysql_query($query); 

    // Loop through the results, and place the results in two arrays 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
    $emotes[] = $row['emote']; 
    $images[] = "<img src='" . $path . "/" . $row['image'] . "'>"; 
    } 

    // The line below replaces the emotes with the images 
    echo str_replace($emotes, $images, $text); 

    $getmsg="SELECT * from tbl_chatmessages a, jcow_accounts b WHERE       a.room_number='$room' && b.username=a.user_alias && a.date='$date' ORDER BY postime DESC"; 
    $getmsg2=mysql_query($getmsg) or die(mysql_error()); 

    while($getmsg3=mysql_fetch_array($getmsg2)) 
    { 

     //$message=Smilify($subject); //Smiley faces 
       print "<table name='tablechat' id='tablechat' cellspacing='0' cellpadding='0' style='margin-top:0px;margin-left:0px;padding:0px;'>"; 
       print "<tr><td rowspan='2'><a href='index.php?p=u/$getmsg3[user_alias]' target='_blank'> 
       <img src='http://www.pinoyarea.com/uploads/avatars/$getmsg3[avatar]' width='50px' height='50px'/></td><td><font color='#333333' style='text-decoration:none;font-size:14px;font-family:tahoma;'><b>&nbsp;$getmsg3[name]</b></font></a>  <font color='#666666' style='text-decoration:none;font-size:10px;font-family:tahoma;'>($getmsg3[time]):</font></td></tr><tr><td><font style='font-family:tahoma;font-size:12px;padding-left:5px;'>".str_replace($emotes, $images, $getmsg3[message])."</font></td></tr>"; 
       print "</table>";  
     } 


     function Smilify(&$subject) 
     { 
      $smilies = array(
      ':D' => 'icon_biggrin', 
      ':)' => 'icon_smile', 
      ':(' => 'icon_sad', 
      ':o' => 'icon_surprised', 
      ':shock:' => 'icon_eek', 
      ':?' => 'icon_confused', 
      ':8' => 'icon_cool', 
      ':lol:' => 'icon_lol', 
      ':x:' => 'icon_mad', 
      ':p' => 'icon_razz', 
      ':red:' => 'icon_redface', 
      ':cry:' => 'icon_cry', 
      ':evil:' => 'icon_evil', 
      ':twisted:' => 'icon_twisted', 
      ':roll:' => 'icon_rolleyes', 
      ':wink:' => 'icon_wink', 
      ':!:' => 'icon_exclaim', 
      ':?:' => 'icon_question', 
      ':idea:' => 'icon_idea', 
      ':arrow:' => 'icon_arrow', 

     ); 

$sizes = array(
    'icon_cry' => 18, 
    'icon_cool' => 20, 
    'haha' => 20, 
    'icon_surprised' => 20, 
    'icon_exclaim' => 20, 
    'icon_razz' => 20, 
    'icon_mad' => 18, 
    'icon_rolleyes' => 20, 
    'icon_wink' => 20, 
); 

    $replace = array(); 
    foreach ($smilies as $smiley => $imgName) 
    { 
     $size = $sizes[$imgName]; 
     array_push($replace, '<img src="images/'.$imgName.'.gif" alt="'.$smiley.'" width="'.$size.'" height="'.$size.'" />'); 
    } 
    $subject = str_replace(array_keys($smilies), $replace, $subject); 
    } 

    ?> 

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script> 
    <script> 
    $(document).ready(function() { 
    $("#tablechat").load("chatlog.php"); 
    var refreshId = setInterval(function() { 
    $("#tablechat").load('chatlog.php?randval='+ Math.random());}, 6000); 
    }); 
    </script> 

답변

0

iframe에로드되는 페이지의 코드입니까? 그렇다면 문제는 아마 그 안에 타이머 코드가 있다는 것입니다. 프레임을 다시로드 할 때마다 새 간격 타이머가 추가됩니다. 타이머가 실행될 때마다 새 타이머가로드됩니다.

해결책은 iframe의 끝에있는 JavaScript를 옮겨서 대신 부모 페이지에 넣는 것입니다.

+0

에 대한 adavenced 기술입니다. –

0

난 항상 당겨을 위해 CometD를 추천하는 가장 좋은 방법은, 그것은 푸시 그래도 문제가 우리가 이미를하고 http://cometd.org/