douguwo2275 2011-04-28 02:40
浏览 62
已采纳

实时更新Web系统 - Ajax,PHP,MySQL

Background: We are launching a event which would be attended by students from various Universities. A web application would report live as to number of students registering for the event.

The basic web layout is

enter image description here

I hope the desired layout is clear. This needs to refresh every 5 mins to display as new students are registered. I have got the refresh part done using Ajax setTimeout() method.

I have table Student. The query I am using is:

  SELECT UNIV, STUDENT_NAME, STUDENT_EMAIL
      FROM STUDENT
      ORDER BY UNIV;

However, the main issue is identifying when to add new row in the table and displaying the details Issue 1: Currently, what I am doing is having $currUniv variable which refers to the current value of University. If the value of $currUniv and current row fetched don't match, I create a new row

Code: in updater.php

function updateTable()
{
   $currUniv = "";
   while($currLine = mysql_fetch_array($results))
   {
      if (strcmp($currLine[0], $currUniv) != 0)
      {
         responseHTML .= "<tr id = \"{$currLine[0]}\">";
         responseHTML .= "<td id = \"{$GLOBALS["univ"]}\">";
         responseHTML .= "{$currLine[0]}";
         responseHTML .= "</td>";
         $currUniv = $currLine[0];
      }
         responseHTML .= "<td id = \"{$GLOBALS["studentname"]}\">";
         responseHTML .= "{$currLine[1]}";
         responseHTML .= "</td>";
         responseHTML .= "<td id = \"{$GLOBALS["studentemail"]}\">";
         responseHTML /= "{$currLine[2]}";
         responseHTML .= "</td>";
      if (strcmp($currLine[0], $currUniv) != 0)
      {
         responseHTML .= "</tr>";
      }
   }
}

Firstly, the table is not displayed. I don't think this is the best algorithm to create the table. Can anyone suggest me any other means of achieving the same?

  • 写回答

1条回答 默认 最新

  • duanfu1945 2011-04-28 03:10
    关注

    Looks like you don't open enough tr and td tags if the university name matches the one from previous row. Make sure there is exactly the same number of tags generated in both cases. Preferably using else. Cells may be empty but you have to generate them.

    Also is there any valid reason you don't sort your results by students name within one university? It's just a small change in the query - ORDER BY UNIV, STUDENT_NAME. Without it you cannot rely on the order given to you by mysql, it will just throw students at whatever order it will think will be the fastest, especially when the table grows big.

    If this is the only thing you need to update it's not complicated enough to give a reason to change your code into something you would have to learn from scratch.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系