2013-02-01 53 views
0

我試圖顯示這個時候有問題需要回答questions計數結果,根據結果顯示信息,利用Ajax顯示信息後,點擊按鈕

刷新頁面,這時候沒有問題,回答。 no questions

這是我已經嘗試使用一個代碼,註釋掉線是,如果環路我試過,但它總是返回1

<?php 
     $dbtype = "mysql"; 
     $dbhost = "localhost"; 
     $dbname = "starsqa"; 
     $dbuser = "root"; 
     try { 
      $pdo_conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser); 
     } catch (PDOException $ex) { 
      echo $ex->getMessage(); 
     } 

     $qry_string = "select * from questions inner join stars on stars.starID = questions.starID where stars.starID = ? && approved = 1 && answered = 1 && `check` = 0"; 
     $prep = $pdo_conn->prepare($qry_string); 
     $starid = $_SESSION['starid']; 
     $prep->execute(array($starid)); 
     //if (count($qry_string) > 0) { 
      //echo count($qry_string); 
      echo "<table style='border:0px; background-color:#8C7E6C;'><thead style='border:0px;'><tr style='border:0px solid white; background-color:#153E7E; color:white; padding: 5; margin: 5;'><th style='border:1px white; padding: 5; margin: 5;'>Question</th><th style='border:1px white; padding: 5; margin: 5;'>Response</th></tr></thead><tbody>"; 
      while ($row = $prep->fetch(PDO::FETCH_ASSOC)) { 
       echo "<tr style='border:1px white; background-color:lightblue; padding: 5; margin: 5;'><td style='border:1px white; padding: 5; margin: 5;'>{$row['question']}</td> 
         <td style='border:1px white; padding: 5; margin: 5;'><textarea rows='4' cols='50' id='{$row['questionID']}' class='response'>{$row['response']}</textarea></td></tr>"; 
      } 
      echo "</tbody></table>";  
      echo "<br><button type='button' class='save_btn'>Save All</button><br><br>"; 
     //} 
//  if (count($qry_string) < 1) { 
//   echo count($qry_string); 
//   echo "no question to answer atm"; 
//  } 
     ?> 

人有,爲什麼這是行不通的想法或另一種方式做到這一點?


它的工作現在(感謝showdev)使用此:

$prep->execute(array($starid)); 
//if (count($qry_string) > 0) { 
    //echo count($qry_string); 
     // $count= count($prep->fetchAll());  
     $count = $prep->rowCount(); 
     //echo "$count"; 
     if ($count > 0){ 
    echo "<table style='border:0px; background-color:#8C7E6C;'><thead style='border:0px;'><tr style='border:0px solid white; background-color:#153E7E; color:white; padding: 5; margin: 5;'><th style='border:1px white; padding: 5; margin: 5;'>Question</th><th style='border:1px white; padding: 5; margin: 5;'>Response</th></tr></thead><tbody>"; 
    while ($row = $prep->fetch(PDO::FETCH_ASSOC)) { 
     echo "<tr style='border:1px white; background-color:lightblue; padding: 5; margin: 5;'><td style='border:1px white; padding: 5; margin: 5;'>{$row['question']}</td> 
       <td style='border:1px white; padding: 5; margin: 5;'><textarea rows='4' cols='50' id='{$row['questionID']}' class='response'>{$row['response']}</textarea></td></tr>"; 
    } 
    echo "</tbody></table>";  
    echo "<br><button type='button' class='save_btn'>Save All</button><br><br>"; 

} 
if ($count < 1) { 
    echo "no question to answer atm"; 
} 




只是想到別的東西了,需要一次我按下保存所有的AJAX刷新頁面,並顯示新/電流信息,我會用什麼功能來做到這一點?

我做到了:-)使用這個:location.reload();

var html = ''; 
$(document).ready(function(){ 
    $(".save_btn").live('click', function() { 

     $('.response').each(function(){ 
      //alert($(this).attr('id')); 
      //alert($(this).val()); 
      if ($(this).val() == '') { 
       html = $.ajax({ 
        url: "response.php?questionID=" + $(this).attr('id') + "&response=" + $(this).val() + "&check=0", 
        async: false 
       }).responseText; 
      } 
      if ($(this).val() !== '') { 
       html = $.ajax({ 
        url: "response.php?questionID=" + $(this).attr('id') + "&response=" + $(this).val() + "&check=1", 
        async: false 
       }).responseText; 
      } 

     }); 
     alert(html); 
     location.reload(); 
    }); 
}) 
+0

使用PDO的道具。 – showdev

+0

幹得好!榮譽尋找你自己的解決方案。您可能會考慮使用表單來發布數據(除非您有特定的理由使用ajax)。看起來更簡單。 – showdev

+0

它開始作爲一種形式,但我改變了,當我決定ajax更好地使用在這種情況下(國際海事組織) – jenstar

回答

1

看起來你正在計算你的查詢字符串而不是結果記錄集。嘗試fetchAll和計數結果,或嘗試rowCount

$count=count($prep->fetchAll();); 
$count = $prep->rowCount(); 

從文檔:

[rowCount時]時,不能確保所有的數據庫,並針對便攜式應用不應加以依賴。

+0

謝謝你現在工作:-)我編輯的問題,以顯示我如何編輯代碼 – jenstar

+0

只是想到了一些東西否則它需要一次我保存所有的ajax刷新頁面,並顯示新的/當前的信息,我會用什麼函數來做到這一點? – jenstar

+0

只需要一行代碼,見上面編輯的問題:-) – jenstar