gpt4 book ai didi

PHP - 查询和循环以显示不同的数据

转载 作者:行者123 更新时间:2023-11-28 16:30:25 24 4
gpt4 key购买 nike

我很难在我的页面中显示不同的新闻。我想在 1 个 div 中显示每个新闻,限制为 4 个新闻。例如,我有 4 个不同的新闻,我希望每个新闻都在 div 中单独显示。我是 html 和 php 的新手。谁能告诉我最好的循环和查询是什么?

很容易理解我想在这里做的是图片。 enter image description here

这就是我正在做的。我想我迷路了。 enter image description here

这是我的新闻 php 代码..

<div class="content">
<div class="container">
<div class="row">

<div class="col-xs-12 col-md-9">
<div class="fix leftbar floatleft">
<div class="fix left_sidebar">
<div class="news">
<h2><i class="fa fa-newspaper-o"></i> Latest News</h2>
<hr class="carved">

<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)) {
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>

<hr>


<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>

<hr>


<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>

<hr>


<div class="fix single_news">
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>

<a href="#"><?php echo $title; ?></a>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
<?php
}
?>
</div>



<a href="#" class="view_news">View More News</a>

</div>
</div>
</div>
</div>

最佳答案

就像你的第一个循环......格式更好

   <?php
include_once('connection.php');
$sql ="SELECT * FROM news ORDER BY news_id ASC";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){
$title = $row['news_title'];
$date = $row['news_date'];
$content = $row['news_content'];
?>
<div class="fix single_news">
<a href="#"><?php echo $title; ?></a>
<div class="single_image">
<img src="img/coveredcourt.jpg" alt="court">
</div>
<p><?php echo $date; ?></p>
<p><?php echo $content; ?></p>
</div>
<hr class="carved"/>
<?php
}
?>

您的循环正在处理所有文章,因此您只需要一个这样的循环。在里面你只需要格式化一篇文章,所有其他的都将采用相同的格式。

关于PHP - 查询和循环以显示不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38055666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com