使用php

时间:2015-12-28 14:26:06

标签: php ajax

下面的代码显示了我的数据库中三列的帖子。

最初,我正在调用6个帖子,所以如果数据库帖子中有4个或5个帖子,则会在表格中显示列,并且有空缺少的帖子(工作正常)。

现在我添加了更多ajax代码,这是长代码,所以我没有在这里发布。现在,如果我的数据库中有7个帖子,当我加载更多帖子来调用第7个帖子时它将覆盖整个表格,而不是它应覆盖表格中有两个空格的一列的空格,如何做到?

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

1 个答案:

答案 0 :(得分:1)

我需要设置一个可以看到它正常工作的地方。

您可以将$columnsPerRow更改为1到1之间的数字,它只是有效。

它是嵌套组。所以,while循环和read ahead对我来说

更改查询限制并享受。

我知道它使用'替代语法'。我尝试了隐藏html generation'. The whole code to generate the table is less 40 lines - It is different with functions and回声的函数,imo,而不是更好,你的选择。这取决于您喜欢的代码。

代码:

<?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');