i want to write a code in php which will sort my array(1,4,3,5,6,2,7,9,8) in ascending and descending order on clicking there respective buttons? How to do this please help.
            Asked
            
        
        
            Active
            
        
            Viewed 1,136 times
        
    -2
            
            
        - 
                    1What you have done so far? code please. – Devsi Odedra Nov 05 '17 at 07:35
- 
                    *"i want to write a code in php"* -- what stops you? – axiac Nov 05 '17 at 07:57
- 
                    "> if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1) { $sql = "SELECT * FROM messages ORDER BY message_id ASC"; }else{ $sql = "SELECT * FROM messages ORDER BY message_id DESC"; } echo $sql; ?> i have don this but its not working – ARITRA PUTATUNDA Nov 05 '17 at 08:45
- 
                    @ARITRAPUTATUNDA Add your source code to your question, not as a comment. – Progman Nov 05 '17 at 09:51
1 Answers
1
            
            
        I offer the following solution:
<form action="" method="post">
<input type="submit" name="but1" value="ASC">
<input type="submit" name="but2" value="DESC">
<?php
$a = [1,4,3,5,6,2,7,9,8];
if (isset ($_REQUEST['but1'])) {
    sort ($a);
    print_r($a);
}
if (isset ($_REQUEST['but2'])) {
    rsort ($a);
    print_r($a);
}
?>
</form>
 
    
    
        Nadiia Soroka
        
- 19
- 1
- 4
