gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:16:30 26 4
gpt4 key购买 nike

我有一个问题。我有一个单页网站,如果发布了一条消息,则帖子会显示在帖子下方,并且帖子下方需要有图片,但在索引页面的最后,我不想显示图片。因此,如果数据库中的 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>";

?>

这部分我不会再深入了,希望你能帮我解决这个问题

最佳答案

很难理解您的需求。但是试试这个查询:

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

?>

关于php - 如何从数据库行中获取最高值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36333578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com