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.
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 examples 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.
Maximum number
9
6
5
4
3
2
1
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