简体   繁体   中英

How to get the highest value from a database row?

I have a problem. I have a one page website and if there is posted a message a posts shows up and below the post there needs to be an image, but on the last on the index page I don't want an image to show up. So it's like if the id from the database is the highest that post doesn't get a image below it.

Here is the code:

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

?>

I won't get any further with this part I hope you can help me with this problem

It's hard to understand what you need. But try this query:

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

Or may be this:

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

Update: this code changes instead of top:

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM