如何从数据库行获取最高值?

时间:2016-03-31 12:30:36

标签: php database pdo

我有问题。我有一个单页网站,如果有帖子显示帖子显示在帖子上方和下方需要有图片,但在索引页面的最后一个我不想要显示图像。所以,如果数据库中的id是最高的,那么帖子在它下面没有图像。

以下是代码:

<?php
include 'functions/image/functions.php';

$countQuery = $db->prepare("SELECT paginaNummer AS max FROM pages ;");
$countRow = $countQuery->fetch();
$maxId = $countRow['max'];

$query = $db->prepare("SELECT * FROM pages");
$query->execute();

$num_cols = 1;
$i = 0;

$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);


while($row = $query->fetch()) {
    if(isset($row)) {
        echo "<section data-stellar-background-ratio='0.5'>";
        echo "<div class='panelContainer'>";
        echo "<div class='panel2'>";
        echo "<div class='symbol3'></div>";
        echo "</div>";
        echo "</div>";
        echo "</div>";
        echo "</section>";
        echo "<section class='wrapper'>";
        echo "<div class='box'>";
        echo "<div class='content'>";
        echo $i++ % $num_cols == 0 ? '' : '';
        echo "<div id='message'><h2>&nbsp;&nbsp;", $row['berichtNaam'], "</h2>";
        echo $row['paginaContent'], "<br />";

        if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
            echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
            echo "&nbsp;<a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
        } elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
            echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
        } else {

        }
        echo "</div>";
        echo "</div>";
        echo "</section>";
        echo max($row);
        if (count($row['paginaNummer']) == max($row)){

        } else {
            echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
        }

        echo "</section>";

    }
}
echo "</div></div>";

?>

我对这部分没有进一步的了解,希望你能帮我解决这个问题

1 个答案:

答案 0 :(得分:3)

很难理解你需要什么。但请尝试此查询:

$countQuery = $db->prepare("SELECT paginaNummer AS max 
                            FROM pages ORDER BY paginaNummer DESC LIMIT 1");

或者可能是这样:

$countQuery = $db->prepare("SELECT paginaNummer AS max 
                            FROM pages ORDER BY id DESC LIMIT 1");

更新:此代码更改而不是顶部:

<?php
include 'functions/image/functions.php';

$query = $db->prepare("SELECT * FROM pages");
$query->execute();
$maxrow = $query->rowCount();

$num_cols = 1;
$i = 0;

$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);

$n = 0;
while($row = $query->fetch()) {
    if(isset($row)) {
       $n++;
        echo "<section data-stellar-background-ratio='0.5'>";
        echo "<div class='panelContainer'>";
        echo "<div class='panel2'>";
        echo "<div class='symbol3'></div>";
        echo "</div>";
        echo "</div>";
        echo "</div>";
        echo "</section>";
        echo "<section class='wrapper'>";
        echo "<div class='box'>";
        echo "<div class='content'>";
        echo $i++ % $num_cols == 0 ? '' : '';
        echo "<div id='message'><h2>&nbsp;&nbsp;", $row['berichtNaam'], "</h2>";
        echo $row['paginaContent'], "<br />";

        if (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 2) {
            echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
           echo "&nbsp;<a class='delete' href='functions/admin/deletePost.php?id=" . $pageNumber . " '>Delete</a>";
        } elseif (isset($_SESSION['login']['5']) && $_SESSION['login']['5'] == 1) {
            echo "&nbsp;<a class='edit' href='functions/admin/edit.php?id=" . $pageNumber . " '>Edit</a>";
        } else {

        }
        echo "</div>";
        echo "</div>";
        echo "</section>";
        echo max($row);
        if ($n == $maxrow){

        } else {
            echo "<a href='/'><img src='<?php echo $path . $img ?>'alt=''/></a>";
        }

        echo "</section>";

    }
}
echo "</div></div>";

?>