使用PHP在Bootstrap轮播中显示动态数据时出现的演示问题

huangapple go评论78阅读模式
英文:

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:

   &lt;!--CAROUSEL TEST --&gt; 
&lt;!--Events panel--&gt;
&lt;!-- Grid row--&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div id=&quot;eventsTitle&quot; align=&quot;center&quot; class=&quot;col-md-12&quot;&gt;
&lt;h1&gt;Events&lt;/h1&gt;       
&lt;/div&gt;
&lt;/div&gt;    
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;row&quot;&gt;           
&lt;div class=&quot;col-md-12&quot;&gt;
&lt;div id=&#39;carouselExampleControls&#39; class=&#39;carousel slide&#39; data-ride=&#39;carousel&#39;&gt;
&lt;div class=&#39;carousel-inner&#39;&gt;
&lt;div class=&#39;carousel-item active&#39;&gt;
&lt;div class=&#39;carousel-caption&#39;&gt;
&lt;!--Fetch and display events from database --&gt;          
&lt;?php
$query = &quot;SELECT * FROM events ORDER BY eventDate LIMIT 3;&quot;;
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) &gt; 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row[&quot;eventName&quot;];
$date = $row[&quot;eventDate&quot;];
$time = $row[&quot;eventTime&quot;];
$loc = $row[&quot;eventPlace&quot;];  
$desc = $row[&quot;eventDesc&quot;]; 
echo &quot; 
&lt;div class=&#39;row&#39;&gt;
&lt;div id=&#39;eventCard&#39; align=&#39;center&#39; class=&#39;col-md-4&#39;&gt;   
&lt;hr&gt;
&lt;h1&gt;$event&lt;/h1&gt;
&lt;br&gt;
&lt;p&gt;&lt;i class=&#39;far fa-calendar-times&#39;&gt;&lt;/i&gt; $date
&lt;br&gt;
&lt;i class=&#39;fas fa-map-marker-alt&#39;&gt;&lt;/i&gt; $loc
&lt;br&gt;
&lt;i class=&#39;far fa-clock&#39;&gt;&lt;/i&gt; $time
&lt;br&gt;
&lt;br&gt;
&lt;a href=&#39;events.php&#39;&gt;More Info &lt;/a&gt;
&lt;/p&gt;
&lt;hr&gt;
&lt;/div&gt;
&lt;/div&gt;
&quot;;
}
}
?&gt;
&lt;/div&gt; &lt;!--end CAROUSEL CAPTION --&gt;
&lt;/div&gt;  &lt;!-- end CAROUSEL ACTIVE --&gt;  
&lt;!-- SECOND CAROUSEL SLIDE --&gt; 
&lt;div class=&#39;carousel-item&#39;&gt;
&lt;div class=&#39;carousel-caption&#39;&gt;
&lt;div class=&#39;row&#39;&gt;
&lt;div id=&#39;eventCard&#39; align=&#39;center&#39; class=&#39;col-md-4&#39;&gt;     
&lt;/div&gt;
&lt;div id=&#39;eventCard&#39; align=&#39;center&#39; class=&#39;col-md-4&#39;&gt;   
&lt;/div&gt;
&lt;div id=&#39;eventCard&#39; align=&#39;center&#39; class=&#39;col-md-4&#39;&gt;      
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt; &lt;!--end CAROUSEL INNER --&gt;
&lt;a class=&#39;carousel-control-prev&#39; href=&#39;#carouselExampleControls&#39; role=&#39;button&#39; data-     slide=&#39;prev&#39;&gt;
&lt;span class=&#39;carousel-control-prev-icon&#39; aria-hidden=&#39;true&#39;&gt;&lt;/span&gt;
&lt;span class=&#39;sr-only&#39;&gt;Previous&lt;/span&gt;
&lt;/a&gt;
&lt;a class=&#39;carousel-control-next&#39; href=&#39;#carouselExampleControls&#39; role=&#39;button&#39; data-slide=&#39;next&#39;&gt;
&lt;span class=&#39;carousel-control-next-icon&#39; aria-hidden=&#39;true&#39;&gt;&lt;/span&gt;
&lt;span class=&#39;sr-only&#39;&gt;Next&lt;/span&gt;
&lt;/a&gt;
&lt;/div&gt; &lt;!--end CAROUSEL CONTROLS --&gt;
&lt;div id=&quot;eventButton&quot; align=&quot;right&quot; class=&quot;col-md-12&quot;&gt;   
&lt;a class=&#39;button&#39; href=&quot;events.php&quot;&gt;&lt;i class=&quot;fas fa-chevron-right&quot;&gt;&lt;/i&gt; MORE EVENTS&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt; &lt;!--end CAROUSEL COLS --&gt;  
&lt;/div&gt; &lt;!--end CAROUSEL ROWS --&gt;
&lt;/div&gt; &lt;!--end CAROUSEL CONTAINER--&gt;
&lt;!-- // end CAROUSEL TEST --&gt;  

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:

使用PHP在Bootstrap轮播中显示动态数据时出现的演示问题

Image example of results when data is retrieved from database using PHP:

使用PHP在Bootstrap轮播中显示动态数据时出现的演示问题

答案1

得分: 1

在你的PHP循环中,你已经添加了&lt;div class=&#39;row&#39;&gt;,所以它会重复出现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 &lt;div class=&#39;row&#39;&gt; added in there so it would be adding this in 3 times, hence it appearing below each other. Try the below perhaps.

&lt;div id=&quot;carouselExampleControls&quot; class=&quot;carousel slide&quot; data-ride=&quot;carousel&quot;&gt;
&lt;div class=&quot;carousel-inner&quot;&gt;
&lt;div class=&quot;carousel-item active&quot;&gt;
&lt;!--Fetch and display events from database --&gt;          
&lt;?php
$query = &quot;SELECT * FROM events ORDER BY eventDate LIMIT 3;&quot;;
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) &gt; 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row[&quot;eventName&quot;];
$date = $row[&quot;eventDate&quot;];
$time = $row[&quot;eventTime&quot;];
$loc = $row[&quot;eventPlace&quot;];  
$desc = $row[&quot;eventDesc&quot;]; 
echo &quot;&lt;div align=&#39;center&#39; class=&#39;col-md-4&#39;&gt;   
&lt;hr&gt;
&lt;h1&gt;$event&lt;/h1&gt;
&lt;br&gt;
&lt;p&gt;&lt;i class=&#39;far fa-calendar-times&#39;&gt;&lt;/i&gt; $date
&lt;br&gt;
&lt;i class=&#39;fas fa-map-marker-alt&#39;&gt;&lt;/i&gt; $loc
&lt;br&gt;
&lt;i class=&#39;far fa-clock&#39;&gt;&lt;/i&gt; $time
&lt;br&gt;
&lt;br&gt;
&lt;a href=&#39;events.php&#39;&gt;More Info &lt;/a&gt;
&lt;/p&gt;
&lt;hr&gt;
&lt;/div&gt;&quot;;
}
}
?&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a class=&quot;carousel-control-prev&quot; href=&quot;#carouselExampleControls&quot; role=&quot;button&quot; data-slide=&quot;prev&quot;&gt;
&lt;span class=&quot;carousel-control-prev-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;sr-only&quot;&gt;Previous&lt;/span&gt;
&lt;/a&gt;
&lt;a class=&quot;carousel-control-next&quot; href=&quot;#carouselExampleControls&quot; role=&quot;button&quot; data-slide=&quot;next&quot;&gt;
&lt;span class=&quot;carousel-control-next-icon&quot; aria-hidden=&quot;true&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;sr-only&quot;&gt;Next&lt;/span&gt;
&lt;/a&gt;

</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

   &lt;?php 
$query = &quot;SELECT * FROM events ORDER BY eventDate LIMIT 3;&quot;;
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) &gt; 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row[&quot;eventName&quot;];
$date = $row[&quot;eventDate&quot;];
$time = $row[&quot;eventTime&quot;];
$loc = $row[&quot;eventPlace&quot;];  
$desc = $row[&quot;eventDesc&quot;]; 
?&gt;
&lt;div class=&#39;row&#39;&gt;
&lt;div id=&#39;eventCard&#39; align=&#39;center&#39;&gt;   
&lt;hr&gt;
&lt;h1&gt;&lt;?php echo $event?&gt;&lt;/h1&gt;
&lt;br&gt;
&lt;p&gt;
&lt;i class=&#39;far fa-calendar-times&#39;&gt;&lt;/i&gt; &lt;?php echo $date; ?&gt;
&lt;br&gt;
&lt;i class=&#39;fas fa-map-marker-alt&#39;&gt;&lt;/i&gt; &lt;?php echo $loc; ?&gt;
&lt;br&gt;
&lt;i class=&#39;far fa-clock&#39;&gt;&lt;/i&gt;&lt;?php echo $time; ?&gt;
&lt;br&gt;
&lt;br&gt;
&lt;a href=&#39;events.php&#39;&gt;More Info&lt;/a&gt;
&lt;/p&gt;
&lt;hr&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;?php
}
}
?&gt;

答案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:

&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;row&quot;&gt;           
&lt;div class=&quot;col-md-12&quot;&gt;
&lt;div id=&#39;carouselExampleControls&#39; class=&#39;carousel slide&#39; data-ride=&#39;carousel&#39;&gt;
&lt;div class=&#39;carousel-inner&#39;&gt;
&lt;div class=&#39;carousel-item active&#39;&gt;
&lt;div class=&#39;carousel-caption&#39;&gt;
&lt;div class=&#39;row&#39;&gt;
&lt;div id=&#39;eventCard&#39; align=&#39;center&#39; class=&#39;col-md-4&#39;&gt;
&lt;!--Fetch and display events from database --&gt;          
&lt;?php
$query = &quot;SELECT * FROM events ORDER BY eventDate LIMIT 3;&quot;;
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) &gt; 0) {
while ($row = mysqli_fetch_assoc($result)) {
$event = $row[&quot;eventName&quot;];
$date = $row[&quot;eventDate&quot;];
$time = $row[&quot;eventTime&quot;];
$loc = $row[&quot;eventPlace&quot;];  
$desc = $row[&quot;eventDesc&quot;]; 
echo &quot; 
&lt;hr&gt;
&lt;h1&gt;$event&lt;/h1&gt;
&lt;br&gt;
&lt;p&gt;&lt;i class=&#39;far fa-calendar-times&#39;&gt;&lt;/i&gt; $date
&lt;br&gt;
&lt;i class=&#39;fas fa-map-marker-alt&#39;&gt;&lt;/i&gt; $loc
&lt;br&gt;
&lt;i class=&#39;far fa-clock&#39;&gt;&lt;/i&gt; $time
&lt;br&gt;
&lt;br&gt;
&lt;a href=&#39;events.php&#39;&gt;More Info &lt;/a&gt;
&lt;/p&gt;
&lt;hr&gt;
&quot;;
}
}
?&gt;
&lt;/div&gt;
&lt;/div&gt;

I have tried it both your suggested way and the way I have posted in this comment, but the data is still stacking vertically.

huangapple
  • 本文由 发表于 2020年1月6日 21:47:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定