2014-10-27 25 views
0

我有一個數據庫表有幾個字段,我想倒數與其中一個字段的整數值(分鐘)。如何循環顯示php表中每個行的倒計時,使用這些值並在過程中添加或減去時間,如果需要的話?Javascript倒數顯示在PHP表

Table.php

  $sql="SELECT * FROM List where depName='Admisiones' and personStatus='Espera'"; 
      $result=mysqli_query($con, $sql); 

      echo "<table border='15' cellpadding='10' cellspacing='3' bordercolor='00FF00'> 
      <tr> 
      <th>Turno</th> 
      <th>Nombre</th> 
      <th>Apellido</th> 
      <th>Segundo Apellido</th> 
      <th>Id Studiante</th> 
      <th>Status</th> 
      <th>Multiples Motivos</th> 
      <th>Tiempo Categoria</th> 
      <th>Tiempo Estimando</th> 
      </tr>"; 

      while($row = mysqli_fetch_array($result)) 
      { 
       echo "<tr>"; 
       echo "<td>" . $row['listNum'] . "</td>"; 
       echo "<td>" . $row['personName'] . "</td>"; 
       echo "<td>" . $row['personLast'] . "</td>"; 
       echo "<td>" . $row['secondLast'] . "</td>"; 
       echo "<td>" . $row['stuId'] . "</td>"; 
       echo "<td>" . $row['personStatus'] . "</td>"; 
       echo "<td>" . $row['manyMotive'] . "</td>"; 
       echo "<td>" . $row['categoryTime'] . "</td>";         
       echo "<td>" . $row['estimatedTime']"</td>"; //USE THE CLOCK.PHP TO MAKE A COUNTDOWN FOR EACH ESTIMATED TIME. 
       //And have the ability to add or substrac time if needed. 
       echo "</tr>"; 
      } 

      echo "</table>"; 

Clock.php

 <script> 
     $('.countdown').each(function(){ 
     var minutes = $(this).data('minutes'); 
     var count = $(this); //countdown 
     var id = $(this).id; 
     $(this).countdown({ 
     date  : Date.now() + (minutes * 60000), 
     refresh : 1000 
     }); 

     setInterval(function(count) 
     var minuteValue = count.text(); 
     $.ajax({ 
      url  : 'TEST.php', //I created to make the test.. 
      type : 'POST', 
      data : {currentMin : minuteValue, id:id}, 
      success : function(response){ 
       console.log(response); 
      } 

      }); 
     }); 
    }); 

test.php的

<?php 

    include 'Connection.php'; 
     $minValue = mysqli_real_escape_string($con, $_POST['minuteValue']); 
     $ID = mysqli_real_escape_string($con, $_POST['id']); 

     if (!$con) { 
      die("Connection failed: " . mysqli_connect_error()); 
     } 

     $sql ="Update List SET estimatedTime='$minValue' WHERE listNum='$ID'"; 
     mysqli_query($con,$sql); 
     mysqli_close($con); 
    ?> 
+0

您很難清楚您要做什麼。你能發表一些代碼並澄清你的問題嗎? – SasaT 2014-10-27 01:21:11

+0

我有一切我只需要一種方法來實現它,所以時鐘採取'$ row ['categoryTime']'的值,並在'$ row ['estimatedTime']'行倒計時。 – 2014-10-27 01:42:56

+0

我不知道'$ row ['categoryTime']'裏面有什麼,但如果我正確理解你的評論,你需要將它傳遞給你的countDownTime函數。所以它看起來像這樣:''並使用該數據做倒計時。我把標籤放在php標籤之外,但如果更容易,你可以連接。如果我完全理解你,仍然不是100%確定。 – SasaT 2014-10-27 02:24:02

回答

0

OK,這裏是我會怎麼做。我會刪除clock.php並使用更復雜的東西。例如這個js插件:

http://rendro.github.io/countdown/

下載文件並將其包含在你的<head>標籤。確保你在之前包含jquery。

裏面你table.php改變這一行:

echo "<td>" . $row['estimatedTime']"</td>"; 

到:

echo "<td><div class=\'countdown\' data-minutes=\'{$row['categoryTime']}\' id=\{$row['id']'\'></div></td>"; 

基本上你爲倒計時場HTML有看起來像這樣(PS:30是動態的數字來自何處'categoryTime'):

<td><div class="countdown" data-minutes="30" id="16"></div></td> 

頁腳或您的javascript文件中包含這個s CRIPT:

<script> 
    $('.countdown').each(function(){ 
     var minutes = $(this).data('minutes'); 
     var id = $(this).id; 
     $(this).countdown({ 
     date  : Date.now() + (minutes * 60000), 
     refresh : 60000 
     }); 

     var minuteValue = $(this).text(); 
     setInterval(function(){ 

      $.ajax({ 
       url  : 'url/to/php/file', //php file that updates your DB. Take the values from POST VAR. 
       type : 'POST', 
       data : {currentMin : minuteValue, id:id}, 
       success : function(response){ 
        console.log(response); 
       } 

      }); 
     }); 
    }); 



</script> 

有了這個,你將環通各.countdown元素,從數據分鐘ATTR得到它分鐘,並告訴倒計時插件時,計時器應通過將這些分鐘Date.now到期()值。您的評論後

編輯:

你所尋找的是node.js中那對你來說是完美的。但我已經更新了代碼。它沒有測試,所以我不知道這是否會奏效。它應該是這樣的。唯一的問題可能(MAYBE)在表格中的每行設置間隔。

EDIT2

    在PHP文件
  • ,在DB
  • 更新行根據您的數據庫運行查詢,看看你能得到的行數影響
  • 如果受影響的行> 0,回聲1,否則回聲0
  • 在Firebug(或類似)檢查控制檯消息,看看你的PHP輸出或任何其他錯誤

這可能會幫助您找到問題的所在

+0

它的工作完美:D謝謝。現在我想知道每次倒數刷新時我是否可以將該值存儲在數據庫中? – 2014-10-27 05:04:28

+0

我也更新了代碼,我做了你說的但不更新。 – 2014-10-28 02:37:22

+0

看看你是否遵循上面提到的那些步驟來試圖找到錯誤 – SasaT 2014-10-28 03:14:39