【问题标题】:Looping through mysql array, populating a slider and I can't figure this out循环遍历 mysql 数组,填充一个滑块,我无法弄清楚
【发布时间】:2015-09-08 21:36:32
【问题描述】:

我正在尝试用 MySQL 填充这个网页设计,但我不知道该怎么做,我知道如何通过关联数组来做一个简单的 while loop,但这似乎很混乱。

我的查询字符串:

$recent_query = "SELECT * FROM RES WHERE ListAgentID='2752' ORDER BY ListDate LIMIT 4";

我的 while 循环来获取行

while ($rf = mysqli_fetch_assoc($recent_result)) {
echo <<<RECENT
<div class="property-row">
  <article class="hentry has-featured">
    <div class="property-featured">
     ROW 1
    </div>
  </article>

  <article class="hentry has-featured">
    <div class="property-featured">

    ROW 2
    </div>
  </article>
</div>
<div class="property-row">
  <article class="hentry has-featured">
    <div class="property-featured">
    ROW 3
    </div>
  </article>

  <article class="hentry has-featured">
    <div class="property-featured">
    ROW 4
    </div>
  </article>
</div>

RECENT;
}

我不知道如何将这 4 篇文章填充为单独的 mysql 行。我想查询 4 ​​行并将它们填充到 HTML 中。我只是似乎做不到。请帮忙。谢谢。

这是滑块的实时示例,在最近的属性下:http://html.nootheme.com/citilights/

【问题讨论】:

  • 什么意思,4 篇文章?您从查询中获得的每条记录都是一篇文章?
  • 滑块有 2 行,每行 2 篇文章,所以 4 篇文章。我只是不知道如何使每篇文章成为单独的 mysql 行。
  • 有人可以帮忙吗?

标签: php mysql


【解决方案1】:

由于您没有在您的桌子上提供任何信息,因此很难知道它的外观。但是从我可以收集到的问题来看,除了您的文章本身之外,您还有一个包含 ListAgentIDListDate 列的表格,我将假设将其称为类似ListArticle.

对,所以 - 你有一个类似于下面的表格

+--------------+-------------+-------------------------------+
|  ListAgentID |  ListDate   | ListArticle                   |
+--------------+-------------+-------------------------------+
| 2752         | 09/09/2015  | Contents of first article...  |
| 2752         | 05/09/2015  | Contents of second article... |
| 2752         | 01/09/2015  | Contents of third article...  |
| 2752         | 30/08/2015  | Contents of fourth article... |
+--------------+-------------+-------------------------------+

因此,当您使用SELECT * FROM RES WHERE ListAgentID='2752' ORDER BY ListDate LIMIT 4 查询时,您将获得存储在表中最后四个条目中的所有信息。这意味着您的 while-loop 必须运行 四次 次才能全部获取。因此,您的代码无法按原样构建。

在我看来,最好的选择是将您从ListArticle-column(或在您的数据库中调用的任何内容)中检索到的文本存储到不同的变量中(使用数组就可以了),然后回显它们稍后会出现在您的页面上,例如:

$posts = array();
while ($rf = mysqli_fetch_assoc($recent_result)) {
    array_push($posts, $row["ListArticle"]);
}

然后你会像这样在你的 HTML 中回显这些变量

<article class="hentry has-featured">
    <div class="property-featured">
        <?php echo $posts[0]; ?>
    </div>
</article>

(注意 PHP 从 0 开始计数,而不是 1,所以如果你想要数组的第一个元素(数据库列表顶部的那个),你必须 echo $posts[0]。)

然后你只需在你想要的地方回显$posts[0]$posts[3]

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多