• 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 Random

Herpicus

Web Developer
Joined
Jun 21, 2008
Messages
1,644
Reaction score
94
Location
British Columbia
Edit Problem Solved Thanks to Holix on IRC :D
Solved:
PHP:
  function cmp($a, $b) {
    global $CFG_sorting_order, $CFG_sorting_type;
    if ($CFG_sorting_order == "ascending")
        {
                return strcasecmp($b['name'], $a['name']);
        }
    if ($CFG_sorting_order == "descending")
        {
                return strcasecmp($a['name'], $b['name']);
        }
	if ($CFG_sorting_order == "random")
        {
                return (mt_rand(0, 1) ? $a['name'] : $b['name']);
        }
  }
  @usort($dirs_raw,  'cmp');
  @usort($files_raw, 'cmp');
Thread can be closed.



So heres my situation, my wallpapers site currently only supports sorting with ascending or descending. I would like my site to show the images randomly.

I've tried using shuffle() but I can't seem to get it to work.

Here is what I have:
PHP:
  function cmp($a, $b) {
    global $CFG_sorting_order, $CFG_sorting_type;
    if ($CFG_sorting_order == "ascending")
        {
                return strcasecmp($b['name'], $a['name']);
        }
    if ($CFG_sorting_order == "descending")
        {
                return strcasecmp($a['name'], $b['name']);
        }
	if ($CFG_sorting_order == "random")
        {
                return shuffle($a['name'], $b['name']);
        }
  }
  @usort($dirs_raw,  'cmp');
  @usort($files_raw, 'cmp');

Here is the original:
PHP:
  function cmp($a, $b) {
    global $CFG_sorting_order, $CFG_sorting_type;
    if ($CFG_sorting_order == "ascending")
        {
                return strcasecmp($b['name'], $a['name']);
        }
    if ($CFG_sorting_order == "descending")
        {
                return strcasecmp($a['name'], $b['name']);
        }
  } // function cmp
  @usort($dirs_raw,  'cmp');
  @usort($files_raw, 'cmp');

Any help is greatly appreciated!
 
Last edited:
In shuffle you just pass one argument which is an array?

It's hard to guess whats the purpose of this program, but first try array_merge() and then shuffle()
 
Back
Top