php - 试图在每个 <ul> 元素中只显示 4 个结果

标签 php css

我有一个 div class="pdfs",我想在其中显示一些可以打开 pdf 的链接。

在这个 div 中我有两个 div,因为我想在左边有一个 pdf block ,在右边有另一个 block ,就像这样:

enter image description here

现在我从数据库中获取 pdf,并且工作正常。

但我只想在我的 <div class="pdfs1"> 中显示最多 4 个 pdf , 如果有超过 4 个 pdf,我想在我的 <div class="pdfs2"> 中显示它们, 以获得正确的链接,就像我在我的图片中一样。

例如,如果我在数据库中有 5 个 pdf,我想在我的 <div class="pdfs1"> 中显示 4 个还有我的一个<div class="pdfs2"> .

我正在尝试使用 if 条件,但它不起作用,你能帮帮我吗?

$readPdfs =  $pdo->prepare("SELECT * FROM pdfs");
$readPdfs->execute();

echo '<div  class="pdfs">';
    echo '<h3>Pdfs to see:</h3>';
    echo '<ul class="pdfs1">';
        while ($readPdfsResult = $readPdfs->fetch(PDO::FETCH_ASSOC))
        {
         $folder  = 'pdfs/';
         echo '<li> <a  href="'.$folder.'/'.$readPdfsResult['pdf'].'">'.$readPdfsResult['pdf'].'</a></li>';
        }
    echo '</ul>';

    echo '<ul class="pdfs2">';
         //but here I want to show only if there are more than 4 pdfs
         echo '<li> <a  href="'.$folder.'/'.$readPdfsResult['pdf'].'">'.$readPdfsResult['pdf'].'</a></li>';
    echo '</ul>';
    echo '</div>';
    echo '</div>';
echo '</div>';

最佳答案

跟踪到目前为止看到的行数,并在显示 4 行时结束每个 UL:

$folder  = 'pdfs/';
$rows = 0;

echo '<ul class="pdfs1">';
while (($rows < 4) && ($readPdfsResult = $readPdfs->fetch(PDO::FETCH_ASSOC)))
{
  $fn = htmlspecialchars($readPdfsResult['pdf']);
  echo "<li> <a href='$folder/$fn'>$fn</a></li>";
  ++$rows;
}
echo '</ul>';

$rows = 0;

echo '<ul class="pdfs2">';
while (($rows < 4) && ($readPdfsResult = $readPdfs->fetch(PDO::FETCH_ASSOC)))
{
  $fn = htmlspecialchars($readPdfsResult['pdf']);
  echo "<li> <a href='$folder/$fn'>$fn</a></li>";
  ++$rows;
}
echo '</ul>';

关于php - 试图在每个 <ul> 元素中只显示 4 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23769470/

相关文章:

php - 由操作系统引起的潜在网站错误?

php - 保护无 session RESTful API 端点

php - 如何使用 1 或 2 个查询构建线程评论?

css - LESS 编译器神奇地将不需要的类添加到我的选择器中

css - 更改 CSS 中的图标字体以获得事件状态

javascript - nivo-lightbox 插件 slideIn images onclick of next or previous

html - 侧边导航全高 100%

php - 登录系统就像stackoverflow的,用php写的

PHP 和 SQL : code is really slow

html - DIV 基线上的超链接