Php 在while循环中导致数组的某些部分消失
下面是一个简短的解释: 我的第一个while从数据库中提取了四个电视节目。我的第二个问题是,当前用户是否对其中任何一个进行了评级。 有趣的是,如果我的第二个while循环没有为从数据库中提取的四个电视节目中的一个找到分数,则未分级的电视节目根本不会显示(因此,如果用户为其中三个节目打分,则第四个未分级的电视节目将消失)。。。为什么呢 下面我有一个广播表单,我想预先检查用户对电视节目的评分(我会检查评分是否存在,并在相应的HTML行中添加“已检查”)Php 在while循环中导致数组的某些部分消失,php,Php,下面是一个简短的解释: 我的第一个while从数据库中提取了四个电视节目。我的第二个问题是,当前用户是否对其中任何一个进行了评级。 有趣的是,如果我的第二个while循环没有为从数据库中提取的四个电视节目中的一个找到分数,则未分级的电视节目根本不会显示(因此,如果用户为其中三个节目打分,则第四个未分级的电视节目将消失)。。。为什么呢 下面我有一个广播表单,我想预先检查用户对电视节目的评分(我会检查评分是否存在,并在相应的HTML行中添加“已检查”) 如果我的第二个while循环没有找到从db中
如果我的第二个while循环没有找到从db中提取的四个电视节目中的一个的分数,则根本不会显示任何内容
因为没有分数的时候你什么都不打印
加:
我想我明白了。这是关于你的第二个while和括号
while($row_score = $score->fetch()) {
var_dump($row_score);
执行此操作时,仅当获取中有内容时,才会打印以下内容。如果用户尚未为该节目打分,fetch()将返回false,表单将不会打印
你能做的一件事就是在这样一个if中获取你的分数
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 ?>
缺少括号,不是吗?唯一的输出行是var\u dump
。还有更多的代码吗?我已经添加了HTML。基本上,如果我把第二个while循环的结束括号放在表单的末尾,我只会得到三个电视节目,而不是四个。我试图改进语言。我的意思是,如果我有$row[1]中第一个查询中的电视节目,而不是四个想要的节目,我只会得到三个。(所以这没用!)谢谢你的建议,但不幸的是,这不起作用。我会放一些屏幕截图,看看发生了什么。如果我把结束括号放在var_转储之后,我会得到这个:,如果我像你一样把它们放在末尾,我会得到这个
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 ?>