基于ID的PHP循环
我目前正在用PHP撰写文章/博客文章 我希望它根据文章的id循环,首先使用最高的值 我的代码基于ID的PHP循环,php,Php,我目前正在用PHP撰写文章/博客文章 我希望它根据文章的id循环,首先使用最高的值 我的代码 <?php include_once('includes/connection.php'); include_once('includes/article.php'); $article = new Article; $articles = $article->fetch_all(); <?php foreach ($articles as $article) { ......
<?php
include_once('includes/connection.php');
include_once('includes/article.php');
$article = new Article;
$articles = $article->fetch_all();
<?php foreach ($articles as $article) {
.........
}
?>
还有我的article.php
<?php
class Article {
public function fetch_all() {
global $pdo;
$query = $pdo->prepare("SELECT * FROM articles");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($article_id) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM articles WHERE article_id = ?");
$query->bindValue(1, $article_id);
$query->execute();
return $query->fetch();
}
}
?>
使用
按物品订购\u id DESC
在查询中,要循环来自SQL数据库的数据集,只需对查询进行排序即可
$query = $pdo->prepare("SELECT * FROM articles ORDER BY article_id DESC");
问题是什么?“请开发解决方案?”不是我们在这里所做的。您所指的最高价值。。按文章id排序?只需在SQL查询中添加
orderbyarticle\u id DESC
。“我想,这就是你想要的。”斯特凡·诺伊伯特我问我该用哪种技术,因为我是新手。但是谢谢你这么好:)