使用php在三列中显示帖子 [英] Displaying posts in three columns using php
问题描述
下面是一段代码,该代码在三栏中显示了我数据库中的帖子.
Below is a code which shows posts from my database in three columns.
最初,我打电话给6个帖子,所以如果数据库帖子中有4或5个帖子,则会在表中占据该列,并且缺少空白的空格(可以正常工作).
Initially, I am calling 6 posts so if there are 4 or 5 posts in database posts will take there column in table and there is empty space for missing post(works fine).
现在,我添加了加载更多的 ajax
代码,这是很长的代码,所以我没有在此处发布.现在,如果我的数据库中有7个帖子,那么当我加载更多帖子以调用第七个帖子时,将显示6个最初被调用的帖子,它覆盖了整个表,而应该覆盖表中一列的空间并带有两个空白空间,该怎么做?
Now I added load more ajax
code which is long code so I didn't post it here. Now if there are 7 posts in my database, 6 being called initially will be displayed on when I load more posts to call seventh posts it covers whole table instead it should cover space of one column in table with two empty spaces, how to do that?
<?php
$sql = "SELECT * FROM posts WHERE status='active' ORDER BY id desc LIMIT 6";
$query = $db->prepare($sql);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
?>
<table cellpadding="5" cellspacing="2">
<tr>
<?php do {
//horizontal looper
?>
<td'>
<div><h2><a href=""><?php echo $row['title']; ?></a></h2></div>
<div><img src='<?php echo $row['pic']; ?>'></div>
<div><h3><?php echo $row['posted']; ?></h3></div></td>
<?php
$row = $query->fetch(PDO::FETCH_ASSOC);
if (!isset($nested_List)) {
$nested_List= 1;
}
if (isset($row) && is_array($row) && $nested_List++%3==0) {
echo "</tr><tr>";
}
} while ($row); //end horizontal looper
?>
</table>
推荐答案
我需要在可以看到其正常工作的地方进行设置.
I need to setup somewhere that you can see it working.
您可以将 $ columnsPerRow
更改为介于1和任何数字之间的数字,这样才可以正常工作.
You can change the $columnsPerRow
to a number between 1 and whatever and it just works.
它是嵌套的组.因此, while
循环并为我预先阅读
It is nested groups. So,while
loops and read ahead
for me
更改查询限制并享受.
我知道它使用替代语法".我尝试过隐藏 html代的功能".生成表的整个代码少于40行-函数和
echo`s imo有所不同,更好的是您的选择.这取决于您喜欢的代码.
I know it uses 'alternative syntax'. I tried functions to hide the html generation'. The whole code to generate the table is less 40 lines - It is different with functions and
echo`s, imo, not better, your choice. It depends on the code you prefer.
代码:
<?php // http://stackoverflow.com/questions/34495342/displaying-posts-in-three-columns-using-php
// This loads a complete 'environment'
// If you comment this out out and ensure a database connection then the code should work
include __DIR__ . '/__bootstrap__.php'; // my setup - including PDO connection
// There is always a PDO database connection - AURA/SQL -- get it: 'appServices('pdo').
/* --------------------------------------------------------------------------
* layout...
*/
$columnsPerRow = 3; // change this as required!
$emptyColumnLiteral = '---'; // this in missing cells
// database connection - PDO - replace this with your PDO
$db = appServices('pdo'); // my application bootstrap knows how to login to the database for me :)
$sql = "SELECT `title`,
`pic_id` AS `pic`,
`posted`
FROM posts
WHERE
`status` = 'active'
ORDER BY
`id` DESC
LIMIT 11";
$query = $db->prepare($sql);
$query->execute();
?>
<table cellpadding="5" cellspacing="2">
<thead>
<?php for ($c = 1; $c <= $columnsPerRow; $c++): ?>
<th>title</th>
<th>pic id</th>
<th>posted date</th>
<?php endfor; ?>
</thead>
<--
Now do the table where each row has $columnsPerRow...
I so like 'read ahead' that you already did - makes it easier...
-->
<?php $row = $query->fetch(PDO::FETCH_ASSOC); ?>
<?php while ($row): ?>
<tr>
<--
Now do one complete row - but we may run out of data
-->
<?php $columnNo = 1; // columns we have printed ?>
<?php while ($row && $columnNo <= $columnsPerRow): // end of data OR end of row ?>
<td><?= $row['title'] ?></td>
<td><?= $row['pic'] ?></td>
<td><?= $row['posted'] ?></td>
<?php $columnNo += 1; // next column to show
$row = $query->fetch(PDO::FETCH_ASSOC); // always 'read ahead''?>
<?php endwhile; ?>
<--
have we completed the row?
-->
<?php while ($columnNo <= $columnsPerRow): // complete the row... ?>
<td><?= $emptyColumnLiteral ?></td>
<td><?= $emptyColumnLiteral ?></td>
<td><?= $emptyColumnLiteral ?></td>
<?php $columnNo += 1; // next column to show ?>
<?php endwhile; ?>
</tr>
<?php endwhile; ?>
</table>
数据库记录:
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'title',
`pic_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'pic',
`posted` datetime NOT NULL,
`status` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'active',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*Data for the table `posts` */
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (1,'title_01','pic_01','2015-12-01 19:58:34','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (2,'title_02','pic_02','2015-12-02 19:59:32','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (3,'title_03','pic_03','2015-12-03 20:00:32','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (4,'title_04','pic_04','2015-12-04 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (5,'title_05','pic_05','2015-12-05 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (6,'title_06','pic_06','2015-12-06 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (7,'title_07','pic_07','2015-12-07 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (8,'title_08','pic_08','2015-12-08 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (9,'title_09','pic_09','2015-12-09 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (10,'title_10','pic_10','2015-12-10 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (11,'title_11','pic_11','2015-12-11 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (12,'title_12','pic_12','2015-12-12 00:00:00','active');
insert into `posts`(`id`,`title`,`pic_id`,`posted`,`status`) values (13,'title_13','pic_13','2015-12-13 00:00:00','active');
这篇关于使用php在三列中显示帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!