【问题标题】:auto update of web page upon update of database更新数据库时自动更新网页
【发布时间】:2011-12-21 19:25:28
【问题描述】:

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

【问题讨论】:

    标签: php jquery ajax refresh reload


    【解决方案1】:

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

    解决方案是将位于 iframe 末尾的 JavaScript 移出 iframe 并将其放在父页面中。

    【讨论】:

      【解决方案2】:

      我推荐 CometD 始终拉动的最佳方式,它是用于推动的先进技术 http://cometd.org/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-25
        • 1970-01-01
        • 2013-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多