The code below displays the images that are stored in the folder, and the path to them is stored in the MySQL database. The request sorts images by the created field and therefore it turns out - ORDER BY images.created DESC .

How can I screw a button here when I click on it, the data will be sorted by ORDER BY images.created ASC , and with one more click everything will return as it was - ORDER BY images.created DESC ?

 <?php $img_url = "uploads/"; $result = mysql_query("SELECT images.id as imgid, images.image_name, images.user_id_fk, users.id, users.login FROM images,users WHERE images.user_id_fk = users.id ORDER BY images.created DESC"); while($row = mysql_fetch_assoc($result)) { echo ' <div class="column is-one-quarter container1"> <a href="img.php?img='.$row['imgid'].'"> <figure class="image image is-square"> <img style="object-fit: cover; cursor: pointer;" src="'.$img_url.$row['image_name'].'" /> </figure> <div class="overlay"> <div class="text">'.$row['login'].'</div> </div> </a> </div> '; } ?> 

    1 answer 1

    Well, for example, by simple.

     <?php if($_GET['sort'] == 1){ echo '<a href="?sort=1" >Сортировать DESC</a>'; $sort = "ASC"; }else{ echo '<a href="?sort=0" >Сортировать ASC</a>'; $sort = "DESC"; } $img_url = "uploads/"; $result = mysql_query("SELECT images.id as imgid, images.image_name, images.user_id_fk, users.id, users.login FROM images,users WHERE images.user_id_fk = users.id ORDER BY images.created ".$sort); while($row = mysql_fetch_assoc($result)) { echo ' <div class="column is-one-quarter container1"> <a href="img.php?img='.$row['imgid'].'"> <figure class="image image is-square"> <img style="object-fit: cover; cursor: pointer;" src="'.$img_url.$row['image_name'].'" /> </figure> <div class="overlay"> <div class="text">'.$row['login'].'</div> </div> </a> </div> '; } ?> 

    It is possible to do without reloading the page via ajax, and this script will be a handler.