2012-01-24 5 views
0

私はmysqlデータベースからいくつかのリレーカードに関するデータをロードするダイナミックなPHPファイルを持っています。すべてのユーザーは異なる量のリレーカードを持っています。すべてのカードには8つのリレー(State 1/0)、8つの入力(State 1/0)、2つのアナログ入力(State of 0〜1024)があり、これはすべてのカードに適用されます。私はできるだけ早くこのデータを更新したいと思います。私はPHPスクリプトを実行して、特定のユーザーのすべてのリレー情報をトリガーしました。この場合、これはユーザー2です。リアルタイムのAJAX/MySQL複数変数ですか?

私が読んでいるものを実行する最良の方法は、jQueryしかし、私はhow-oが多くの変数を更新するのを理解していません。

ここでは、私は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のこと、私は完全にそれを逃す!!

Thanx a lot!

答えて

0

あなたは可能性があり、ライトJS機能(私は右のあなたを理解している場合、あなたが更新されたデータを取得するために特定の時間間隔でAJAX reqestsを送信する必要が
...抽象的な質問... PHPコードが不要のよりこの答えはあなたの質問、それ、あなたのデータで何をしたいかの状態を確認してくださいとは何の関係も持っていない場合はhttp://www.w3schools.com/ajax/
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ここ
はAJAXに関するリンクがある:のsetInterval())
ここ はのsetIntervalについてのリンクがありますか?各アップデートに表示しますか?またはどのような更新プログラムですか?私はそれをリンクする理由

+0

は[w3fools](http://w3fools.com)ポイントにMARX B W3cshools @ –

+0

取得者の権利とその非常によく組織... thatsのは...私が間違った情報をspotedことはありませんそこに...多分古いものもあるかもしれないが...まだ彼らは仕事をしている – Tudor

+0

実際、私はAJAXには本当に新しいですが、かなり早くexempleから学びます!私がしたいことは、上記のphp phileを1秒間隔で実行し、htmlページ内のすべての情報を更新することです。 – Speedy2k

0
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. 
+0

正直言って、私は大きなJavaプログラマではありませんし、私はかなり早くexempleで学びます。あなたが望むなら私はソースcoeにwebdavアクセスを与えることができます。 – Speedy2k

関連する問題