0

簡単な説明は次のとおりです。

私の最初の期間は、データベースから 4 つのテレビ番組を引き出します。2 つ目は、現在のユーザーがそれらのいずれかを評価したかどうかを調べます。面白いことに、私の 2 番目の while ループが、データベースから取得した 4 つのテレビ番組のいずれかのスコアを見つけられない場合、評価されていないテレビ番組はまったく表示されません (したがって、ユーザーがそのうちの 3 つにスコアを付けた場合、 4 番目の評価されていないものは消えます。)... どうしてですか?

以下に、ユーザーがテレビ番組に付けたスコアを事前にチェックするラジオ フォームがあります (スコアが存在するかどうかをチェックし、対応する HTML 行に「checked」を追加します)。

<?php
try {
    $conn = new PDO('mysql:host=localhost;dbname=mytvbox', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

    $stmt = $conn->prepare('SELECT * FROM shows WHERE id > (SELECT MAX(id) - 4 FROM shows)');
    $stmt->execute(array('id' => $id));

    while($row = $stmt->fetch()) {



    $show_id = $row[0];

    $requete = "SELECT * FROM show_score WHERE show_id = $show_id  AND user_id =  $user_id";
    $score = $conn->prepare($requete);
    $score->execute(array('show_id' => $show_id));

    while($row_score = $score->fetch()) {
            var_dump($row_score);


?>


<div id="index-last-shows" class="three columns">
<div class="tip">
    <a href="<?php echo $row[0];  ?>"><img src="<?php echo $row[4];  ?>" /></a>
</div>
<div class="tip-content">   
    <div class="tip-container">
        <div class="tip-header">
            <h1>   <?php echo $row[1];  ?>      </h1>
        </div>
        <div class="row">
            <div class="twelve columns">
             <div id="flash"></div>
                <form id="<?php echo $row[0]; ?>">
                    <div class="your-score">
                        <div class="">Your Score</div> <div id="flash"></div>
                         <input class="hover-star" type="radio" name="tvshowrating" value="1" title="1"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="2" title="2"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="3" title="3"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="4" title="4"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="5" title="5"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="6" title="6"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="7" title="7"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="8" title="8"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="9" title="9"/>   
                         <input class="hover-star" type="radio" name="tvshowrating" value="10" title="10"/>    
                         <input type="hidden" id="show_id-<?php echo $row[0]; ?>" value="<?php echo $row[0]; ?>" /> 
                         <input type="hidden" id="user_id-<?php echo $row[0]; ?>" value="<?php echo $user_id ?>" />
                         <span id="hover-test" style="margin:0 0 0 20px;"></span>
                         <input id="submitscore" type="submit" value="Submit scores!" onclick="addScore(<?php echo $row[0]; ?>);" />  
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

編集:言葉をもう少し明確に。

編集 2: 何が起こっているかのスクリーンショットをいくつか載せます。var_dump の後に閉じかっこを置くと、http://i.imgur.com/CrnsnIe.jpgが得られます。必要なように最後に置くと、http://i.imgurが得られます。 .com/zVqMOiX.jpg

4

2 に答える 2

1

2 番目の while ループで、データベースから取得した 4 つのテレビ番組のいずれかのスコアが見つからない場合、何も表示されません。

スコアがないときは何も印刷しないからですか?

追加:

if ($score->num_rows() === 0 {
    echo "no score";
}
于 2013-05-10T16:54:49.600 に答える
1

私はそれを得ると思います。それはあなたの 2 番目のwhileと括弧についてです。

while($row_score = $score->fetch()) {
    var_dump($row_score);

これを行うと、フェッチに何かがある場合にのみ、以下のコンテンツが出力されます。ユーザーがショーを採点していない場合、fetch()はfalseを返し、フォームは印刷されません。

あなたができることの1つは、そのような場合にスコアを取得することです

if($row_score = $score->fetch())
    var_dump($row_score);

あなたが私たちに与えたコードでは、おそらく次のようになります:

<?php
try {
    $conn = new PDO('mysql:host=localhost;dbname=mytvbox', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

    $stmt = $conn->prepare('SELECT * FROM shows WHERE id > (SELECT MAX(id) - 4 FROM shows)');
    $stmt->execute(array('id' => $id));

    while($row = $stmt->fetch()) {

        $show_id = $row[0];

        $requete = "SELECT * FROM show_score WHERE show_id = $show_id  AND user_id =  $user_id";
        $score = $conn->prepare($requete);
        $score->execute(array('show_id' => $show_id));

        if($row_score = $score->fetch())
            var_dump($row_score);


?>    

<div id="index-last-shows" class="three columns">
<div class="tip">
    <a href="<?php echo $row[0];  ?>"><img src="<?php echo $row[4];  ?>" /></a>
</div>
<div class="tip-content">   
    <div class="tip-container">
        <div class="tip-header">
            <h1>   <?php echo $row[1];  ?>      </h1>
        </div>
        <div class="row">
            <div class="twelve columns">
             <div id="flash"></div>
                <form id="<?php echo $row[0]; ?>">
                    <div class="your-score">
                        <div class="">Your Score</div> <div id="flash"></div>
                         <input class="hover-star" type="radio" name="tvshowrating" value="1" title="1"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="2" title="2"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="3" title="3"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="4" title="4"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="5" title="5"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="6" title="6"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="7" title="7"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="8" title="8"/>
                         <input class="hover-star" type="radio" name="tvshowrating" value="9" title="9"/>   
                         <input class="hover-star" type="radio" name="tvshowrating" value="10" title="10"/>    
                         <input type="hidden" id="show_id-<?php echo $row[0]; ?>" value="<?php echo $row[0]; ?>" /> 
                         <input type="hidden" id="user_id-<?php echo $row[0]; ?>" value="<?php echo $user_id ?>" />
                         <span id="hover-test" style="margin:0 0 0 20px;"></span>
                         <input id="submitscore" type="submit" value="Submit scores!" onclick="addScore(<?php echo $row[0]; ?>);" />  
                    </div>
                </form>
            </div>
<?php } //End While
} //End Try ?>
于 2013-05-10T17:45:00.010 に答える