如何在while循环的外部和内部回显查询结果的第一行2次或更多次?
以下代码只回显循环外的第一行,并且不允许在循环内再次回显
case '1':
$db->Query("SELECT titulo_video,subtitulo_video,youtube_id,nome_norma FROM videos");
echo "<h2>" . $db->Row()->nome_norma . "</h2>";
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
break;
我想要的数据如下:
<h3>row 1 - colum 1</h3>
<h2>row1 - colum 2</h2>
<p>row 1 - colum 3</p>
<h2>row2 - colum 2</h2>
<p>row 2 - colum 3</p>
<h2>row3 - colum 2</h2>
<p>row 4 - colum 3</p>
答案 0 :(得分:2)
你可以稍微摆弄一下。
if ($row = $db->Row ())
{
echo '<h2>' ...;
do
{
echo '<h3>' ...;
echo '<p>' ...;
} while ($row = $db->Row ());
}
答案 1 :(得分:1)
使用 Sentinel value :
$i = 0;
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
if($i==0){
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
$i++;
}
答案 2 :(得分:0)
如果你真的想以不同方式处理第一行,完全在循环之外处理它,而不是在while循环中包含条件,那么处理它就更清晰:
$first_row = $db->Row();
echo "<h2>" . $first_row->nome_norma . "</h2>";
echo "<h3>" . $first_row->titulo_video . "</h3>";
echo "<h3>" . $first_row->subtitulo_video . "</h3>";
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
break;