Welcome to Techno Smarter QA, Questions, Answers Community
+2 votes
78 views
What PHP code can arrange and display the numeric values from the MYSQL database from the MAX down to the MIN?
in PHP by (210 points) 2 11
You can use while loop and retrieve data from max to minimum in PHP from the MYSQL database.
Use ORDER BY id DESC in your query and get max to min values.

2 Answers

+2 votes

This is a simple method to fetch and display maximum value to a minimum in PHP using While loop and array. I am going to share two methods. These methods will help you retrieve max values to min values from the MYSQL database using PHP.  

How to fetch and display maximum value to the minimum in PHP? 

Method 1 - 

You can use order by id DESC to fetch and display maximum value to minimum values and while loop to get all values from maximum to minimum. 

Let's have a look - 

<?php 

   $result = mysqli_query($mysqli, "SELECT * FROM table_name ORDER BY id DESC "); 

while($res = mysqli_fetch_array($result)) { 

        echo $res['id'].'<br>'; 

?> 

The example above will help you fetch and display all maximum values to minimum values 

Method 2 - 

Use array() . 

<?php 

$data = array();

 $result =mysqli_query($mysqli, "SELECT  id FROM table_name ORDER BY id DESC");

while ($res = mysqli_fetch_array($result)) { 

    $data[] = $res['id'];

    echo $res['id'].'<br>';

}

if (count($data) > 0) {

    echo 'maximum: '.$data[0].'<br>';

    echo 'minimum: '.end($data).'<br>';

} else {

     echo 'No values found '.'<br>';

}

?>

In both example above  - 

id - id is a table row 

table _name  - Specify your table name 

In this way, you can get max value to min in PHP with an MYSQL database. 

by (1.4k points) 2 5
edited by
+1 vote

Maximum number 

9

6

5

4

3

2

Minimum number 

1. Use ORDER by id DESC 

where DESC - Descending Order that retrieves max value first. 

2. Use max() and min() function then use While loop to fetch and display max value to min value in PHP 

like - id<mylastmaxvalue 

        id>myfirstminvalue 

by (4k points) 3 24 52
...