• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] Sort functions (Good for AAC)

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
Hey :) Was making some systems etc and I'm always too lazy to make sort (when you click e.g. Name and it sort by name) :/ So I decided to make some functions so it will take seconds to configure this sorting :D

Now, here are some functions (if you use gesior's AAC, place in config-and-functions.php):
PHP:
function makeOrder($arr, $order, $default) {
	// Function by Colandus!
	$type = 'asc';
	if(isset($_GET['order'])) {
		$v = explode('_', strrev($_GET['order']), 2);
		if(count($v) == 2)
			if($orderBy = $arr[strrev($v[1])])
				$default = $orderBy;
				$type = (strrev($v[0]) == 'asc' ? 'desc' : 'asc');
	}
	
	return 'ORDER BY ' . $default . ' ' . $type;
}

function getOrder($arr, $order, $this) {
	// Function by Colandus!
	$type = 'asc';
	if($orderBy = $arr[$this])
		if(isset($_GET[$order])) {
			$v = explode('_', strrev($_GET[$order]), 2);
			if(strrev($v[1]) == $this)
				$type = (strrev($v[0]) == 'asc' ? 'desc' : 'asc');
		}
	
	return $this . '_' . $type;
}

How to use?? Simple.

First you make an array, like this for example:
PHP:
$order = array(
	"guild" => "name", 
	"versus" => "target_name",
	"status" => "end",
	"started_at" => "start"
);

The key means what will be displayed, and the value is the database field name. Which means if I have "order=guild" it will order by field "name" in database.

Then in your SQL query you do like this:
PHP:
$warList = $SQL->query('SELECT * FROM guild_history ' . makeOrder($order, 'order', 'end'));

First parameter is the array, second is the GET-name ('order' means $_GET['order']) and last ('end') is default sort...

Now to the final part, to display the links:
PHP:
$main_content .= '
	<table border=0 cellspacing=1 cellpadding=4 width=100%>
		<tr bgcolor="'.$config['site']['vdarkborder'].'">
			<td align="center"><a href="index.php?subtopic=war&order=' . getOrder($order, 'order', 'guild') . '" class=white>Guild</a></td>
			<td align="center"><a href="index.php?subtopic=war&order=' . getOrder($order, 'order', 'versus') . '" class=white>Versus</a></td>
			<td align="center"><a href="index.php?subtopic=war&order=' . getOrder($order, 'order', 'status') . '" class=white>Status</a></td>
			<td align="center"><a href="index.php?subtopic=war&order=' . getOrder($order, 'order', 'started_at') . '" class=white>Started at</a></td>
			<td align="center"><b><font color="white">Kills</font></b></td>
		</tr>
			' . $war_rows . '
	</table>
';

First parameter is array, second id GET-name, last is what will be in GET (e.g. &order=guild)...

The point of adding getOrder and not writing yourself is because it will make auto asc, desc.

So it will become like &order=guild_asc, and next time you click it will become &order=guild_desc


IF YOU LIKE IT, DON'T FORGET REP++ :thumbup:


Regards,
Colandus
 
bump best function!!!

download maybe i release table.php skriptek o_O
 
Actually it isn't a good way of sorting things, since you have to do the same querys for both results. A better way is caching this data and returning on a RESTful data.
 
You need to do the same query twice to get asc and desc results. There is better ways to do that.
 
Back
Top