从数据库表中检索随机唯一行 6 次,以填充和替换 HTML 中的占位符字段


Retrieving a random unique row from the database table, 6 times, to populate and replace placeholder fields in HTML

>我正在开发一个虚构的活动网站,该网站应该显示各种管理员创建的事件,我希望为每个特色事件生成独特的页面。

<div class="row">
   <div class="4u 12u$(mobile)">
      <article class="item">
         <a href="#" class="image fit"><img src="../images/pic02.jpg" alt="" /></a>
         <header>
            <h3>Event 1</h3>
         </header>
      </article>
      <article class="item">
         <a href="#" class="image fit"><img src="../images/pic03.jpg" alt="" /></a>
         <header>
            <h3>Event 2</h3>
         </header>
      </article>
   </div>
   <div class="4u 12u$(mobile)">
      <article class="item">
         <a href="#" class="image fit"><img src="../images/pic04.jpg" alt="" /></a>
         <header>
            <h3>Event 3</h3>
         </header>
      </article>
      <article class="item">
         <a href="#" class="image fit"><img src="../images/pic05.jpg" alt="" /></a>
         <header>
            <h3>Event 4</h3>
         </header>
      </article>
   </div>
   <div class="4u$ 12u$(mobile)">
      <article class="item">
         <a href="#" class="image fit"><img src="../images/pic06.jpg" alt="" /></a>
         <header>
            <h3>Event 5</h3>
         </header>
      </article>
      <article class="item">
         <a href="#" class="image fit"><img src="../images/pic07.jpg" alt="" /></a>
         <header>
            <h3>Event 6</h3>
         </header>
      </article>
   </div>
</div>

我不想Event 1, Event 2 ...我想在数据库中拥有events表中的实际eventName

我之前使用过以下代码来生成一个包含数据库中字段的表:

<?php
    $queryEvents = "SELECT * FROM events GROUP BY eventStart DESC";
    $resultEvents = $mysqli->query($queryEvents);
?>
<table class="tableList" align="left">
<tr>
</tr>
<?php
while($rowEvents = $resultEvents->fetch_assoc()){
    echo "<tr>";
    echo "<td>{$rowEvents['eventName']}</td>";
    echo "<td><a href='"../details.php?eventID={$rowEvents['eventID']}'">View</a></t
    d>";
    echo "<td><a href='"edit.php?eventID={$rowEvents['eventID']}'">Edit</a></td>";
    echo "<td><a href='"delete.php?eventID={$rowEvents['eventID']}'">Delete</a></td>";
    echo "</tr>";
}
?>
</table>

但是,这仅适用于数据库中的所有值。如何只检索 6 个随机值,并且每个值都是唯一的,以替换Event 1(等(占位符?

检索随机事件的新查询:

$queryEvents = "SELECT * FROM events ORDER BY RAND() LIMIT 6";
$resultEvents = $mysqli->query($queryEvents);

然后,在HTML中,像这样:

<?php while( $rowEvents = $resultEvents->fetch_assoc() ): ?>
    <div class="4u 12u$(mobile)">
        <article class="item">
            <a href="#" class="image fit"><img src="../images/pic04.jpg" alt="" /></a>
            <header>
                <h3><?php echo $rowEvents['eventName'] ?></h3>
            </header>
        </article>
    </div>
<?php endwhile; ?>

但是每个<div>中有 2 个<article>,所以你可以使用一个标志,比如(即(这样:

<?php
$flag = True;
while( $rowEvents = $resultEvents->fetch_assoc() ): ?>
<?php if( $flag ): ?>
    <div class="4u 12u$(mobile)">
<?php endif; ?>
        <article class="item">
            (...)
        </article>
<?php if( $flag ): ?>
    </div>
<?php
    endif;
    $flag = ! $flag;
?>
在开始时,$flag设置为 True,因此在第一个循环中,

<div></div> 将打印出来;在第一个循环结束时,$flag设置为 False,因此在下一个循环中<div>不会打印出来,在第二个循环结束时,$flag设置为 True, 等。。。

您的数据库查询:

$queryEvents = $mysqli->query("SELECT * FROM events ORDER BY RAND() LIMIT 6");
$resultEvents = mysqli->fetch_assoc($queryEvents);

用于显示数据的 foreach 循环:

foreach ($resultEvents as $event) {
    echo "<tr>";
    echo "<td>{$event['eventName']}</td>";
    echo "<td><a href='"../details.php?eventID={$event['eventID']}'">View</a></t
    d>";
    echo "<td><a href='"edit.php?eventID={$event['eventID']}'">Edit</a></td>";
    echo "<td><a href='"delete.php?eventID={$event['eventID']}'">Delete</a></td>";
    echo "</tr>";
}