更新数据库时自动更新网页

时间:2011-11-04 07:30:13

标签: php jquery ajax refresh reload

我目前遇到一些问题。我正在创建一个带有聊天功能的网站。截至目前,我们能够更新消息在用户提交消息时出现的区域。但是我们遇到了一些问题。我们使用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>

2 个答案:

答案 0 :(得分:0)

这是加载到iframe中的页面的代码吗?如果是这样,问题可能是你有里面的计时器代码。每次重新加载帧时,你都会添加一个新的间隔定时器,每次定时器运行时都会加载一个新的定时器,依此类推。

解决方案是将最终存在的JavaScript移出iframe并将其放在父页面中。

答案 1 :(得分:0)

我推荐使用CometD的最佳方式是拉动,它是推动的adavenced技术 http://cometd.org/