0

我有一个选择 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

注意:尝试重复尝试以查看它是否会起作用,但它显示的输出与输出相同,但第二个输出是重复的,除了说 News 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.
4

1 回答 1

0

问题是你用每个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, and$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]的假设$number是从 开始1,并且数组是0基于


编辑这里是回显您的 html 的方法。访问 php 文档会很有帮助,因为所有这些都在那里。在这些我设置$number0,但您可以将其更改回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>";
     }
    ?>
于 2013-07-15T16:59:25.707 回答