Php 使用AJAX/MySQL多变量实现实时性?
我有一个dymamic PHP文件,从MySQL数据库加载关于某个中继卡的数据,每个用户都有不同数量的中继卡。所有卡都有8个继电器(状态1/0)、8个输入(状态1/0)和2个模拟输入(状态从0到1024)。这是对于每个卡,一些用户将有1个继电器,而另一些用户可以有8个继电器,我希望尽快更新此数据。我已经完成了一个PHP脚本,该脚本触发特定用户的所有中继信息,在本例中,这是用户2 我认为按照我所读到的那样做我想做的事情的最好方法是从jQuery调用它,但我不理解如何更新许多变量 下面是我必须从MySQL获取最新状态的代码。 好的,这里是代码的更新,我已经完全分离了数据获取和GUI创建,所以我想做的是,实时显示get_states_GUI.php文件从mysql获取到GUI.php文件的内容。既然我现在完全迷路了,有人能给我指个好方向吗!THanx这里有两个文件: gui.php:Php 使用AJAX/MySQL多变量实现实时性?,php,mysql,ajax,real-time,Php,Mysql,Ajax,Real Time,我有一个dymamic PHP文件,从MySQL数据库加载关于某个中继卡的数据,每个用户都有不同数量的中继卡。所有卡都有8个继电器(状态1/0)、8个输入(状态1/0)和2个模拟输入(状态从0到1024)。这是对于每个卡,一些用户将有1个继电器,而另一些用户可以有8个继电器,我希望尽快更新此数据。我已经完成了一个PHP脚本,该脚本触发特定用户的所有中继信息,在本例中,这是用户2 我认为按照我所读到的那样做我想做的事情的最好方法是从jQuery调用它,但我不理解如何更新许多变量 下面是我必须从My
<?php
require_once('config.php');
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
// We create connection to the MySQL database.
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$con) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database\n");
}
$req="SELECT * FROM cartes WHERE utilisateur=$user";
$result = mysql_query($req);
if(!$result) {
die('Query failed: ' . mysql_error());
}
$nb_iplab = mysql_num_rows($result);
$b = "1";
while ($ligne = mysql_fetch_assoc($result))
{
extract($ligne);
${'nom'.$b} = $nom;
${'mac'.$b} = $mac;
${'ip'.$b} = $ip;
for ($i=1; $i <= "8"; $i++ )
{
//We set Relays names for each IPLAB and get their states in the MySQL database.
${'nomR'.$i.$b} = ${'nomR'.$i};
$req = "SELECT * FROM states_log WHERE dev='R$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateR'.$i.$b} = $state;
if ( ${'stateR'.$i.$b} == "1" )
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=CR$i target=empty> <img src=img/toggle_1.png height=28></a>";
}
else if ( ${'stateR'.$i.$b} == "0" )
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=SR$i target=empty> <img src=img/toggle_0.png height=28></a>";
}
else
{
${'img_linkR'.$i.$b} = "<a href=set_states.php?ip=${'ip'.$b}&cmd=CR$i target=empty> <img src=img/LED_yellow.png height=28></a>";
}
//We set Inputs names for each IPLAB and get their states in the MySQL database.
${'nomI'.$i.$b} = ${'nomI'.$i};
${'multI'.$i.$b} = ${'multI'.$i};
${'img_onI'.$i.$b} = ${'img_onI'.$i};
${'img_offI'.$i.$b} = ${'img_offI'.$i};
${'img_naI'.$i.$b} = ${'img_naI'.$i};
$req = "SELECT * FROM states_log WHERE dev='I$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateI'.$i.$b} = $state;
if ( ${'stateI'.$i.$b} == "1" )
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_onI'.$i.$b} height=28>";
}
else if ( ${'stateI'.$i.$b} == "0" )
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_offI'.$i.$b} height=28>";
}
else
{
${'img_linkI'.$i.$b} = "<img src=img/${'img_naI'.$i.$b} height=28>";
}
// We check for how many times, the Inputs has changed state to 1, and we set them as a variable for counter.
$req = "SELECT count(*) FROM states_log WHERE dev='I$i' AND state='1' AND mac='${'mac'.$b}'";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
list (${'countI'.$i.$b}) = mysql_fetch_row ($result2);
if (isset(${'multI'.$i.$b}))
{
${'countI'.$i.$b} = ${'countI'.$i.$b} * ${'multI'.$i.$b};
}
}
//We set Analog names fir each IPLAB and get their states in the MySQL database.
for ($i=1; $i <= "3"; $i++ )
{
${'nomA'.$i.$b} = ${'nomA'.$i};
${'unitA'.$i.$b} = ${'unitA'.$i};
$req = "SELECT * FROM states_log WHERE dev='A$i' AND mac='${'mac'.$b}' ORDER BY date_heure DESC LIMIT 1";
$result2 = mysql_query($req);
if(!$result2)
{
die('Query failed: ' . mysql_error());
}
$ligne = mysql_fetch_assoc($result2);
extract($ligne);
${'stateA'.$i.$b} = $state;
}
if ( debug == "1" )
{
echo "${'nom'.$b}<br>";
echo "MAC = ${'mac'.$b}<br>";
for ( $i = 1; $i <= 8; $i++ )
{
echo "${'nomR'.$i.$b} = ${'stateR'.$i.$b}<br>";
echo "${'nomI'.$i.$b} = ${'stateI'.$i.$b}<br>";
}
for ( $i = 1; $i <= 3; $i++ )
{
echo "${'nomA'.$i.$b} = ${'stateA'.$i.$b}<br>";
}
echo "---------------------------------------<br>";
}
$b++;
}
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
if ( debug == "1" )
{
echo 'Page generated in '.$total_time.' seconds.'."\n";
}
mysql_close($con);
?>
你可以写更多的抽象问题。。。不需要php代码…
如果我没弄错的话,您需要在特定的时间间隔发送ajax请求以获取更新的数据(js函数setInterval())
这里有一个关于setInterval的链接:
下面是关于ajax的链接:
如果此答案与您的问题无关,请检查并说明您希望如何处理数据?在每次更新时显示它?或者是什么样的更新?@MARX B W3C学校说得对,组织得很好……这就是为什么我把它联系起来。。。我从没有发现任何错误的信息。。。也许有些过时的。。。但它们仍然有效是的,事实上我对AJAX真的很陌生,但从示例中学习的速度相当快!我想做的是以1秒的间隔执行上面的php phile并更新html页面中的所有信息。你愿意在这方面帮助我吗?老实说,我不是一个大JAVA程序员,我通过示例学习得很快。如果您愿意,我可以让webdav访问源coe。?
You could use javascript to trriger ajax script after 1 sec delay use setTimeout() in javascript for delay And in your php page retrieve the data and echo it.
You could use javascript to trriger ajax script after 1 sec delay use setTimeout() in javascript for delay And in your php page retrieve the data and echo it.