Live Update Web system - 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
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?
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.
精彩评论