我有一个动态的PHP文件,它从MySQL数据库加载关于一些继电器卡的数据,每个用户都有不同数量的继电器卡。所有卡都有8个继电器(状态1/0),8个输入(状态1/0)和2个模拟输入(状态从0到1024),这是针对每个卡的,一些用户将有1个,其他一些可以有8个我想尽快更新这些数据。我已经做了一个PHP脚本,触发所有的特定用户的继电器信息,在这种情况下,这是用户2.实时与AJAX/MySQL多个变量?
我认为最好的方式做我想要的,因为我已经阅读是从jQuery调用它,但我不明白如何更新许多变量。
这是我必须从MySQL获取最新状态的代码。 好的,这里是代码的更新,我完全分离了数据获取和GUI创建,所以我想要做的是实时显示get_states_gui.php文件从mysql获取到gui.php文件。有人能指引我走向好的方向,因为我现在完全失去了!感谢名单了很多这里的2个文件: 的gui.php:
<?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);
?>
而现在的get_states_gui.php:
<?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);
?>
我可以给访问源中,MySQL等,如果有人能够帮助,我现在正在学习,现在我真的很喜欢它,但AJAX Jquery的事情,我完全错过了!
非常感谢!
[w3fools(http://w3fools.com) –
@马克思乙W3cshools得到的给点权及其组织的非常好...这就是为什么我说挂...我从来没有spoted任何错误信息那里......也许有些过时......但他们仍然在努力 – Tudor
是的,事实上,我对AJAX来说真的很陌生,但是从例证中学得非常快!我想要做的是以1秒的间隔执行上面的php phile并更新html页面中的所有信息。 – Speedy2k