英文:
Presentation issue when displaying dynamic data using PHP inside a Bootstrap Carousel
问题
I understand that you want the code to be translated, but not the questions. Here's the translated code:
我正在为网站首页创建一个动态事件日历,使用PHP从数据库中显示。我还使用Bootstrap框架(版本4.4.1)构建了一个轮播图,以每个轮播项以3个事件的方式显示。
当从数据库中显示多个事件时,我的问题出现了,检索到的数据垂直堆叠而不是水平排列。但是,如果我在HTML中硬编码三个“事件”到轮播中,那么这些事件会以水平方式正确格式化。
我尝试了几种方法,仍然在寻找解决方案方面遇到了困难,所以我将非常感激任何帮助。
HTML/PHP:
<!--轮播测试-->
<!--事件面板-->
<!--网格行-->
<div class="row">
<div id="eventsTitle" align="center" class="col-md-12">
<h1>事件</h1>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id='carouselExampleControls' class='carousel slide' data-ride='carousel'>
<div class='carousel-inner'>
<div class='carousel-item active'>
<div class='carousel-caption'>
<!--从数据库获取和显示事件-->
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
echo "
<div class='row'>
<div id='eventCard' align='center' class='col-md-4'>
<hr>
<h1>$event</h1>
<br>
<p><i class='far fa-calendar-times'></i> $date
<br>
<i class='fas fa-map-marker-alt'></i> $loc
<br>
<i class='far fa-clock'></i> $time
<br>
<br>
<a href='events.php'>更多信息 </a>
</p>
<hr>
</div>
</div>";
}
}
?>
</div>
</div>
</div>
</div>
<!--轮播控件-->
<a class='carousel-control-prev' href='#carouselExampleControls' role='button' data-slide='prev'>
<span class='carousel-control-prev-icon' aria-hidden='true'></span>
<span class='sr-only'>Previous</span>
</a>
<a class='carousel-control-next' href='#carouselExampleControls' role='button' data-slide='next'>
<span class='carousel-control-next-icon' aria-hidden='true'></span>
<span class='sr-only'>Next</span>
</a>
<div id="eventButton" align="right" class="col-md-12">
<a class='button' href="events.php"><i class="fas fa-chevron-right"></i> 更多事件</a>
</div>
</div>
</div>
</div>
CSS:
#eventCard {
padding: 3em;
}
/*测试轮播*/
body {
background-color: #fff;
}
.carousel {
margin-top: 0px;
}
.carousel-inner {
height: 350px;
}
.carousel-caption {
color: #242424;
top: 50%;
}
I hope this helps! If you have any more questions or need further assistance, please feel free to ask.
英文:
I am creating a dynamic events calendar for a website homepage which displays from the database using PHP. I am also using the Bootstrap framework (version 4.4.1) to construct a carousel in which to display the upcoming events 3 at a time per carousel-item.
My problem arrises when more than one event is displayed from the database. The retrieved data is stacked vertically instead of horizontally. However, if I hardcode three 'events' inside the carousel using HTML, the events are formatted correctly in a horizontal manner.
I have tried several approaches and I am still struggling to find a solution, so I would greatly appreciate any help.
HTML/PHP:
<!--CAROUSEL TEST -->
<!--Events panel-->
<!-- Grid row-->
<div class="row">
<div id="eventsTitle" align="center" class="col-md-12">
<h1>Events</h1>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id='carouselExampleControls' class='carousel slide' data-ride='carousel'>
<div class='carousel-inner'>
<div class='carousel-item active'>
<div class='carousel-caption'>
<!--Fetch and display events from database -->
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
echo "
<div class='row'>
<div id='eventCard' align='center' class='col-md-4'>
<hr>
<h1>$event</h1>
<br>
<p><i class='far fa-calendar-times'></i> $date
<br>
<i class='fas fa-map-marker-alt'></i> $loc
<br>
<i class='far fa-clock'></i> $time
<br>
<br>
<a href='events.php'>More Info </a>
</p>
<hr>
</div>
</div>
";
}
}
?>
</div> <!--end CAROUSEL CAPTION -->
</div> <!-- end CAROUSEL ACTIVE -->
<!-- SECOND CAROUSEL SLIDE -->
<div class='carousel-item'>
<div class='carousel-caption'>
<div class='row'>
<div id='eventCard' align='center' class='col-md-4'>
</div>
<div id='eventCard' align='center' class='col-md-4'>
</div>
<div id='eventCard' align='center' class='col-md-4'>
</div>
</div>
</div>
</div>
</div> <!--end CAROUSEL INNER -->
<a class='carousel-control-prev' href='#carouselExampleControls' role='button' data- slide='prev'>
<span class='carousel-control-prev-icon' aria-hidden='true'></span>
<span class='sr-only'>Previous</span>
</a>
<a class='carousel-control-next' href='#carouselExampleControls' role='button' data-slide='next'>
<span class='carousel-control-next-icon' aria-hidden='true'></span>
<span class='sr-only'>Next</span>
</a>
</div> <!--end CAROUSEL CONTROLS -->
<div id="eventButton" align="right" class="col-md-12">
<a class='button' href="events.php"><i class="fas fa-chevron-right"></i> MORE EVENTS</a>
</div>
</div> <!--end CAROUSEL COLS -->
</div> <!--end CAROUSEL ROWS -->
</div> <!--end CAROUSEL CONTAINER-->
<!-- // end CAROUSEL TEST -->
CSS:
#eventCard {
padding: 3em;
}
/*TEST CAROUSEL */
body{
background-color: #fff;
}
.carousel{
margin-top: 0px;
}
.carousel-inner{
height: 350px;
}
.carousel-caption{
color: #242424;
top: 50%;
}
Image example of desired result from HTML hardcoding of event data:
Image example of results when data is retrieved from database using PHP:
答案1
得分: 1
在你的PHP循环中,你已经添加了<div class='row'>
,所以它会重复出现3次,因此会显示在彼此下面。也许可以尝试以下代码:
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<!-- 从数据库获取并显示事件 -->
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
echo "<div align='center' class='col-md-4'>
<hr>
<h1>$event</h1>
<br>
<p><i class='far fa-calendar-times'></i> $date
<br>
<i class='fas fa-map-marker-alt'></i> $loc
<br>
<i class='far fa-clock'></i> $time
<br>
<br>
<a href='events.php'>More Info </a>
</p>
<hr>
</div>";
}
}
?>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
请注意,我只翻译了你提供的HTML和PHP代码部分,没有包含其他内容。
英文:
In your PHP loop, you have the <div class='row'>
added in there so it would be adding this in 3 times, hence it appearing below each other. Try the below perhaps.
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<!--Fetch and display events from database -->
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
echo "<div align='center' class='col-md-4'>
<hr>
<h1>$event</h1>
<br>
<p><i class='far fa-calendar-times'></i> $date
<br>
<i class='fas fa-map-marker-alt'></i> $loc
<br>
<i class='far fa-clock'></i> $time
<br>
<br>
<a href='events.php'>More Info </a>
</p>
<hr>
</div>";
}
}
?>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
答案2
得分: 1
将HTML代码移到PHP echo之外。有时我也会遇到这种情况。出于某种原因,这样做有效。尝试像这样:
CSS样式
.carousel-caption .row{
display: inline-flex;
width: 200px;
}
PHP循环
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
?>
<div class='row'>
<div id='eventCard' align='center'>
<hr>
<h1><?php echo $event;?></h1>
<br>
<p>
<i class='far fa-calendar-times'></i> <?php echo $date; ?>
<br>
<i class='fas fa-map-marker-alt'></i> <?php echo $loc; ?>
<br>
<i class='far fa-clock'></i><?php echo $time; ?>
<br>
<br>
<a href='events.php'>更多信息</a>
</p>
<hr>
</div>
</div>
<?php
}
}
?>
英文:
Put the html code out of php echo. Some time this happens to me also. For some reason this works.
try like this
CSS Style
.carousel-caption .row{
display: inline-flex;
width: 200px;
}
PHP Loop
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
?>
<div class='row'>
<div id='eventCard' align='center'>
<hr>
<h1><?php echo $event?></h1>
<br>
<p>
<i class='far fa-calendar-times'></i> <?php echo $date; ?>
<br>
<i class='fas fa-map-marker-alt'></i> <?php echo $loc; ?>
<br>
<i class='far fa-clock'></i><?php echo $time; ?>
<br>
<br>
<a href='events.php'>More Info</a>
</p>
<hr>
</div>
</div>
<?php
}
}
?>
答案3
得分: 0
以下是您要翻译的代码部分:
@PHPology
Amended source code:
<div class="container">
<div class="row">
<div class="col-md-12">
<div id='carouselExampleControls' class='carousel slide' data-ride='carousel'>
<div class='carousel-inner'>
<div class='carousel-item active'>
<div class='carousel-caption'>
<div class='row'>
<div id='eventCard' align='center' class='col-md-4'>
<!--Fetch and display events from database -->
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
echo "
<hr>
<h1>$event</h1>
<br>
<p><i class='far fa-calendar-times'></i> $date
<br>
<i class='fas fa-map-marker-alt'></i> $loc
<br>
<i class='far fa-clock'></i> $time
<br>
<br>
<a href='events.php'>More Info </a>
</p>
<hr>";
}
}
?>
</div>
</div>
希望这对您有所帮助。如果您需要任何进一步的翻译,请随时告诉我。
英文:
@PHPology
Amended source code:
<div class="container">
<div class="row">
<div class="col-md-12">
<div id='carouselExampleControls' class='carousel slide' data-ride='carousel'>
<div class='carousel-inner'>
<div class='carousel-item active'>
<div class='carousel-caption'>
<div class='row'>
<div id='eventCard' align='center' class='col-md-4'>
<!--Fetch and display events from database -->
<?php
$query = "SELECT * FROM events ORDER BY eventDate LIMIT 3;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row["eventName"];
$date = $row["eventDate"];
$time = $row["eventTime"];
$loc = $row["eventPlace"];
$desc = $row["eventDesc"];
echo "
<hr>
<h1>$event</h1>
<br>
<p><i class='far fa-calendar-times'></i> $date
<br>
<i class='fas fa-map-marker-alt'></i> $loc
<br>
<i class='far fa-clock'></i> $time
<br>
<br>
<a href='events.php'>More Info </a>
</p>
<hr>
";
}
}
?>
</div>
</div>
I have tried it both your suggested way and the way I have posted in this comment, but the data is still stacking vertically.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论