0

当有问题要回答问题时,我试图证明这一点

当没有问题要回答时。没有问题

这是我尝试使用的代码,注释掉的行是我尝试过的 if 循环,但它总是返回 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();
    });
})
4

1 回答 1

1

看起来您正在计算查询字符串而不是结果记录集。尝试fetchAll计算结果或尝试rowCount

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

从文档中:

[rowCount] 不保证适用于所有数据库,不应依赖于可移植应用程序。

于 2013-02-01T23:04:16.360 回答