AJAX按查询排序[关闭]

I am looking to add a 'sort by' option... when the page first loads its shows all the content throw this function:

function latest($n) {
$dbCon = mysqli_connect("localhost", "artlibr1_default", "azerty123", "artlibr1_01");
$sql = "SELECT * FROM general ORDER BY date_added DESC LIMIT $n, 1";
$query = mysqli_query($dbCon, $sql);
while ($list = mysqli_fetch_assoc($query)) {
 return $list['path'];
}

But if i want to be more precise in the search i can can, for example, only want to show elements where type="painting".

So for that i would need to change my function's sql statement to something like:

$sql = "SELECT * FROM general WHERE type="painting" ORDER BY date_added DESC LIMIT $n, 1";

I imagine i need to do an AJAX but realy don't see how to get this done :/ ( i have done simple AJAX calls before) If you guys could help me out, it would be great !

You could do an ajax request and then update the data on your page. However, depending how much data you are displaying/filtering you may find the quickest and most eloquent solution is to use a js filter plugin such as:

Isotope: http://isotope.metafizzy.co/ or TinySort: http://tinysort.sjeiti.com/

They will keep the existing data in the DOM, but show/hide what the user sees. That means it will be very quick as you're not doing additional calls.