0

Here's a quick explanation:

My first while pulls four tv shows from the database. My second one finds if the current user has rated any of them. Funny thing is, if my second while loop doesn't find a score for one of the four tv shows pulled from the db, the unrated tv show doesn't display at all (so if the user gave a score to three of them, the fourth unrated one disappears.)... Why is that ?

Below I have a radio form where I want to precheck the score a user gave to the tv show (I would check if the score exists and add "checked" to the corresponding HTML line).

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

Edit: language a bit more clear.

Edit 2 : I'll put some screen shots of what's happening. If I put the closing brackets after the var_dump I get this: https://i.sstatic.net/frvV9.jpg, if I put them at the end like I need them to be I get this https://i.sstatic.net/34YDp.jpg

4
  • 1
    There are missing brackets, no ? Commented May 10, 2013 at 16:54
  • The only outputting line there is the var_dump. Is there more code to this? Commented May 10, 2013 at 16:54
  • I've added the HTML. Basically, if I put the closing bracket from the second while loop at the end of the form, I only get three tv shows instead of four. Commented May 10, 2013 at 17:10
  • Tried to improve language. Commented May 10, 2013 at 17:25

2 Answers 2

1

I think I get it. It's about your second while and brackets.

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

When you do that, the content below will be printed only if there is something in the fetch. If the user hasn't scored the show, fetch() will return false and the form will not be printed.

One thing you can do is to fetch your score in a if like that

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

With the code you gave us, it will be like that maybe :

<?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 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this but unfortunately this doesn't work. I'll put some screen shots of what's happening. If I put the closing brackets after the var_dump I get this: i.imgur.com/CrnsnIe.jpg, if I put them at the end like you did I get this i.imgur.com/zVqMOiX.jpg
1

if my second while loop doesn't find a score for one of the four tv shows pulled from the db, nothing displays at all

Because you don't print anything when there is no score?

Add:

if ($score->num_rows() === 0 {
    echo "no score";
}

1 Comment

What I meant is that, if I have my tvshows from the first query in $row[1], instead of having the four wanted shows, I only get three. (So this doesn't help!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.