我想用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/
由于您没有在表格上提供任何信息,因此很难知道它看起来是什么样子。但是从你的问题中我可以收集到,除了你的文章本身,你有一个包含ListAgentID
和ListDate
列的表,我将假设被称为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
-循环必须运行四次才能获取它们。正因为如此,你的代码不能按原来的方式构建。
在我看来,最好的选择是将您从ListArticle
列(或在您的数据库中调用的任何内容)检索到的文本存储到不同的变量中(使用数组可以做到这一点),然后在您的页面上稍后回显它们,如下所示:
$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]
!