当前位置 : 主页 > 网络编程 > 其它编程 >

使用Ajax实时更改数据库-RealtimedatabasechangeswithAjax

来源:互联网 收集:自由互联 发布时间:2023-07-02
ImbuildingawebsitethatprintsthecontentinitsMysqldatabasetothepagefortheusertose I'm building a website that prints the content in it's Mysql database to the page for the user to see. The database's content is going to be constantly added to,
ImbuildingawebsitethatprintsthecontentinitsMysqldatabasetothepagefortheusertose

I'm building a website that prints the content in it's Mysql database to the page for the user to see. The database's content is going to be constantly added to, and I want to show those changes in real time on the page without the user having to reload. I'm using PHP to echo the contents of the database to the page right now and it works great, it's just that to see any new changes, the page has to be reloaded. So my question is, how do I make the page update itself in real time? I'm guessing this is going to involve Ajax but I'm rather new to Javascript...

我正在建立一个网站,它将Mysql数据库中的内容打印到页面上,供用户查看。数据库的内容将被不断地添加到,我想在页面上实时显示这些变化,而不需要用户重新加载。我现在正在使用PHP将数据库的内容返回到页面,它工作得很好,只是要看到任何新的更改,页面必须重新加载。我的问题是,如何让页面实时更新?我猜这将涉及到Ajax,但我对Javascript很陌生……

Would you guys mind pointing me in the right direction?

你们能帮我指出正确的方向吗?

Here's what my database looks like:

我的数据库是这样的:

id author body----------------------------------------1 jim sample content2 bob more content3 fred some more!

I'm using the following PHP code to print the above data to the web page:

我使用下面的PHP代码将上述数据打印到web页面:

$query = mysql_query("SELECT * FROM log order by id desc") or die(mysql_error());while($row = mysql_fetch_array($query)) : echo $row['author']; echo $row['body'];endwhile;

Thanks!

谢谢!

2 个解决方案

#1

3

If you want to use the jQuery library, you can use this example I wrote. Its pretty daunting at first, but ill explain the best I can.

如果您想使用jQuery库,可以使用我编写的这个示例。一开始挺吓人的,但我会尽我所能解释清楚。

test2.php

test2.php

So basically, it checks if the document is ready and everything is loaded. After that is makes a new array called shownIds. This variable will keep the id (primary index from your sql table) to make sure it doesn't show the same data twice. SetInterval just makes it call that function every two seconds. $.get this function gets the page source of test2.php which has the json data. It parses it with $.parseJSON and then loops through each. To make sure that no two rows are shown twice, it checks with $.inArray to see if the id is already in the shownIds array. If it isnt, it appends the data (just id for now) onto the only div. Then it pushes the id into the shownIds array so that it doesnt get shown again.

基本上,它会检查文档是否准备好,所有内容都已加载。然后创建一个名为shownIds的新数组。这个变量将保持id(来自sql表的主索引),以确保它不会两次显示相同的数据。SetInterval让它每两秒调用一次函数。美元。获取该函数获取test2的页面源。有json数据的php。它与$解析。parseJSON然后循环。为了确保没有两个行显示两次,它使用$进行检查。inArray查看id是否已经在shownIds数组中。如果不是,它会将数据(暂时只是id)附加到惟一的div中。

test2.php makes an array. Sets the ids, and echos the data in the json format (not needed but keeps it organized)

test2。php数组。设置id,并以json格式对数据进行回声处理(不需要,但要保持有序)

#2

1

Use jquery ajax http://api.jquery.com/jQuery.ajax/ its simple and easy and call ajax method after every second...so that after every second u will get new data.

使用jquery ajax http://api.jquery.com/jQuery.ajax/它简单易用,每过一秒钟就调用ajax方法……这样每一秒钟之后,u就会得到新的数据。

网友评论