Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于ID的PHP循环_Php - Fatal编程技术网

基于ID的PHP循环

基于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撰写文章/博客文章

我希望它根据文章的id循环,首先使用最高的值

我的代码

<?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
。“我想,这就是你想要的。”斯特凡·诺伊伯特我问我该用哪种技术,因为我是新手。但是谢谢你这么好:)