×

Please Login or Register to continue.

2
(1.7K Views)

What PHP code can arrange and display the numeric values from the MYSQL database from the MAX down to the MIN?
(210 Points)
in PHP

Share

2 Answers
(1.4K Points)
(edited)
2

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 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. 

 

 


Comment

(4.3K Points)
(edited)
1
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 

 


Comment

Related Questions :-

Featured Items:-


Certificate and Marksheet system with verification & Franchise in PHP

$75



Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65




Your Answer

You can post your answer after login..

Live Chat

Hello! How can we assist you today?