从具有多行的查询中一次选择一行


Select one row at a time from a query with multiple rows?

我有一个选择3行的查询;但我只想一次显示一行,使用该行的数据,然后移动到下一行,然后重复。这将如何实现?表中有4项。

PHP:

<?php
$server = "secret";
$user = "secret";
$pass = "secret";
$db = "secret";
$con=mysqli_connect($server,$user,$pass,$db);
if (mysqli_connect_errno($con))
   {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
$sql = 'SELECT * FROM news ORDER BY id DESC LIMIT 3';
mysqli_select_db($con, $db);
$query = mysqli_query($con, $sql);
$number = 1;
while ($result = mysqli_fetch_array($query)) { 
    $id = $result['id'];
    $title = $result['title'];
    $news_date = $result['news_date'];
    $post = $result['post'];
}
mysqli_close($con);
?> 

尝试显示:

        <div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
        <p>News <?php echo $number; ?></p>
        <p><?php echo $post; ?></p>
        <?php   
           $number++;
        ?>

尝试输出:

Test
 || 
7/14/13
News 1
Testing to see if this will work lalalalalla

注意:尝试重复该尝试,看看它是否有效,但它显示的与输出相同,但第二个是重复的,除了新闻2

所需输出外观:

Newest Title | Newest Date
Newest news. Etc. Insert Big Paragraph of news here etc etc.
2nd Newest Title | 2nd Newest Date
2nd Newest news. Etc. Insert Big Paragraph of news here etc etc.
3rd Newest Title | 3rd Newest Date
3rd Newest news. Etc. Insert Big Paragraph of news here etc etc.

问题是,您正在用每个while()循环重写变量-

while ($result = mysqli_fetch_array($query)) { 
    $id = $result['id'];
    $title = $result['title'];
    $news_date = $result['news_date'];
    $post = $result['post'];
}

因此,当您尝试回显$id$title$news_date$post时,您将只得到最后一行数据

你要么需要在while循环中添加你的html-

while ($result = mysqli_fetch_array($query)) { 
    $id = $result['id'];
    $title = $result['title'];
    $news_date = $result['news_date'];
    $post = $result['post'];
    ?>
   <div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
    <p>News <?php echo $number; ?></p>
    <p><?php echo $post; ?></p>
    <?php   
       $number++;
}

或者将它们保存到阵列

$id = array();
$title = array();
$news_date = array();
$post = array();
while ($result = mysqli_fetch_array($query)) { 
    $id[] = $result['id'];
    $title[] = $result['title'];
    $news_date[] = $result['news_date'];
    $post[] = $result['post'];
}

然后访问显示器中的阵列

    <div name='title'><?php echo $title[$number-1]; ?></div> || <div name='news_date'><?php echo $news_date[$number-1]; ?></div>
    <p>News <?php echo $number; ?></p>
    <p><?php echo $post[$number-1]; ?></p>
    <?php   
       $number++;
    ?>

我使用[$number-1],因为我假设$number1开始,并且阵列是基于0


edit以下是回显html的方法。访问php文档将是有益的,因为所有这些都在那里。在这些中,我将$number设置回0,但您可以将其更改回1,只需更改$number->$number-1$number+1->$number 中的每一个

使用for循环-http://php.net/manual/en/control-structures.for.php

<?php
for($number=0;$number<count($title);$number++){
?>
    <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
    <p>News <?php echo $number+1; ?></p>
    <p><?php echo $post[$number]; ?></p>
<?php   
}
?>

使用foreach循环-http://php.net/manual/en/control-structures.foreach.php

<?php
foreach($title as $number => $value{
?>
    <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
    <p>News <?php echo $number+1; ?></p>
    <p><?php echo $post[$number]; ?></p>
<?php   
}
?>

使用while循环-http://php.net/manual/en/control-structures.while.php

<?php
$number = 0;
while($number<count($title){
?>
    <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
    <p>News <?php echo $number+1; ?></p>
    <p><?php echo $post[$number]; ?></p>
<?php   
    $number++;
}
?>

以上每一项都是通过关闭php,编写html,然后再次打开php来完成的。您也可以只停留在php中,然后回显html-http://php.net/manual/en/function.echo.php

<?php
for($number=0;$number<count($title);$number++){
    echo "<div name='title'>".$title[$number]."</div> || <div name='news_date'>".$news_date[$number]."</div>";
    echo "<p>News ".($number+1)."</p>";
    echo "<p>".$post[$number]."</p>";
     }
    ?>