2011-11-04 56 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中的頁面的代碼?如果是這樣,問題可能是你的代碼裏面有了定時器。每次重新加載幀時,您都要添加一個新的間隔計時器,每當該計時器運行時,它都會加載一個新的計時器,等等。

解決方案是將您在那裏獲得的JavaScript移出iframe,並將其放在父頁面中。

+0

我們已經做了這一點,同樣的事情發生。 –

0

我推薦的cometd爲常拉的最佳方式,它是adavenced技術推動 http://cometd.org/