0

这是一个快速的解释:

我第一次从数据库中提取了四个电视节目。我的第二个发现当前用户是否对其中任何一个进行了评分。有趣的是,如果我的第二个 while 循环没有找到从数据库中提取的四个电视节目之一的分数,则根本不会显示未分级的电视节目(因此,如果用户给其中三个评分,第四个未评级的消失了。)...这是为什么?

下面我有一个广播表单,我想在其中预先检查用户给电视节目的分数(我会检查分数是否存在并将“已检查”添加到相应的 HTML 行)。

<?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

我想我明白了。这是关于你的第二个while和括号。

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

当你这样做时,只有在获取内容时才会打印下面的内容。如果用户没有为节目评分,fetch()将返回false并且不会打印表单。

您可以做的一件事是在这样的情况下获取您的分数

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 回答
1

如果我的第二个 while 循环没有找到从数据库中提取的四个电视节目之一的得分,则根本不会显示任何内容

因为没有分数时你不打印任何东西?

添加:

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