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

Sort function in php?? [Modern AAC]

tosse12

Panchira Project Member
Joined
Jun 10, 2007
Messages
864
Reaction score
9
Location
Sweden
Hi members of OtLand!

I am having some problems to create a sort fuction in my code (For spells)

PHP:
<style type="text/css">
td {
    color: white;
    height: 20px;
    color: #666;
}

</style>
<?php 

if(!defined('BASEPATH')) exit('No direct script access allowed'); 

$DIR = '*:/*/*/data/spells/'; 
     
if(is_dir($DIR)) { 
    $spells = simplexml_load_file($DIR.'spells.xml'); 
	$table_color_1 = 'black';
	$table_color_2 = '#080808';
?> 
<h2>Spells</h2>
<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%">
<tr  bgcolor="#101010 ">
<th style="color: #FF4500">Spell Name</th>
<th style="color: #FF4500">Words</th>
<th style="color: #FF4500">Mana Cost</th>
<th style="color: #FF4500">Level Required</th>
</tr>

<?php

$i = 1;
foreach($spells as $spell) {
$color = ($i % 2 ? $table_color_1 : $table_color_2);
$i++;

    echo '
	<tr bgcolor="'.$color.'" id="spell_list">
		<td style="font-size: 10pt;">'.$spell['name'].'</td>
		<td style="font-size: 10pt;">'.$spell['words'].'</td>
		<td style="font-size: 10pt;">'.$spell['mana'].'</td>
		<td style="font-size: 10pt;">'.$spell['lvl'].'</td>
	</tr>';
}

}
else{alert('Error while loading spells. Directory address must be correct.');}
?>
</table>
I am not sure where and how I shall add the sort function.

I would like that Spell Name to be sorted.

If I am unclear, Could you please tell me which part that's unclear!

Thanks in advance!
 
There you go:

PHP:
<style type="text/css"> 
td { 
    color: white; 
    height: 20px; 
    color: #666; 
} 

</style> 
<?php  

if(!defined('BASEPATH')) exit('No direct script access allowed');  

$DIR = '*:/*/*/data/spells/';  
      
if(is_dir($DIR)) {  
    $spells = simplexml_load_file($DIR.'spells.xml');  
    $table_color_1 = 'black'; 
    $table_color_2 = '#080808'; 
?>  
<h2>Spells</h2> 
<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%"> 
<tr  bgcolor="#101010 "> 
<th style="color: #FF4500">Spell Name</th> 
<th style="color: #FF4500">Words</th> 
<th style="color: #FF4500">Mana Cost</th> 
<th style="color: #FF4500">Level Required</th> 
</tr> 

<?php 
sort($spells);
$i = 1; 
foreach($spells as $spell) { 
$color = ($i % 2 ? $table_color_1 : $table_color_2); 
$i++; 

    echo ' 
    <tr bgcolor="'.$color.'" id="spell_list"> 
        <td style="font-size: 10pt;">'.$spell['name'].'</td> 
        <td style="font-size: 10pt;">'.$spell['words'].'</td> 
        <td style="font-size: 10pt;">'.$spell['mana'].'</td> 
        <td style="font-size: 10pt;">'.$spell['lvl'].'</td> 
    </tr>'; 
} 

} 
else{alert('Error while loading spells. Directory address must be correct.');} 
?> 
</table>
 
I have already tried that<_<
Code:
A PHP Error was encountered

Severity: Warning

Message: sort() expects parameter 1 to be array, object given

Filename: pages/spells.php

Line Number: 30
 
Try, if syntax errors let me know, i have no way to check here:

PHP:
<style type="text/css">  
td {  
    color: white;  
    height: 20px;  
    color: #666;  
}  

</style>  
<?php   

if(!defined('BASEPATH')) exit('No direct script access allowed');   

$DIR = '*:/*/*/data/spells/';   
       
if(is_dir($DIR)) {   
    $spells = simplexml_load_file($DIR.'spells.xml');   
    $table_color_1 = 'black';  
    $table_color_2 = '#080808';  
?>   
<h2>Spells</h2>  
<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%">  
<tr  bgcolor="#101010 ">  
<th style="color: #FF4500">Spell Name</th>  
<th style="color: #FF4500">Words</th>  
<th style="color: #FF4500">Mana Cost</th>  
<th style="color: #FF4500">Level Required</th>  
</tr>  

<?php  
sort($spells); 
$i = 1;  
$j = 0;
foreach($spells as $spell) {  
$color = ($i % 2 ? $table_color_1 : $table_color_2);  
$i++;  

$name = $spell['name'];
$words = $spell['words'];
$mana = $spell['mana'];
$lvl = $spell['lvl'];
$array[$j] = "$name,$words,$mana,$lvl";
$j = $j + 1;

}

$manual = 1;
foreach($array as $var) {  
	$manual = $manual + 1;
}

for ($k = 1; $k <= $manual; $k++) {
if ($k == 1)
{
	$get = $array[0];
}else{
	$get = "$get,$array[$k]";
}


}

$show = explode(",", $get);

$phantomVar = 0;
for ($ForShow = 0; $ForShow <= 1000; $ForShow++) {
	$showp1 = $phantomVar + 1;
	$showp2 = $phantomVar + 2;
	$showp3 = $phantomVar + 3;

    echo '  
    <tr bgcolor="'.$color.'" id="spell_list">  
        <td style="font-size: 10pt;">'.$show[$phantomVar][.'</td>  
        <td style="font-size: 10pt;">'.$show[$showp1].'</td>  
        <td style="font-size: 10pt;">'.$show[$showp2].'</td>  
        <td style="font-size: 10pt;">'.$show[$showp3].'</td>  
    </tr>';  
	check = $show[$showp3];
	
	if !($check > 0)
	{
		$ForShow = 1200;
	}
	$phantomVar = $phantomVar + 4;
}


}  
else{alert('Error while loading spells. Directory address must be correct.');}  
?>  
</table>
 
Last edited:
PHP:
<style type="text/css">   
td {   
    color: white;   
    height: 20px;   
    color: #666;   
}   

</style>   
<?php    

if(!defined('BASEPATH')) exit('No direct script access allowed');    

$DIR = '*:/*/*/data/spells/';    
        
if(is_dir($DIR)) {    
    $spells = simplexml_load_file($DIR.'spells.xml');    
    $table_color_1 = 'black';   
    $table_color_2 = '#080808';   
?>    
<h2>Spells</h2>   
<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%">   
<tr  bgcolor="#101010 ">   
<th style="color: #FF4500">Spell Name</th>   
<th style="color: #FF4500">Words</th>   
<th style="color: #FF4500">Mana Cost</th>   
<th style="color: #FF4500">Level Required</th>   
</tr>   

<?php   
sort($spells);  
$i = 1;   
$j = 0; 
foreach($spells as $spell) {   
$color = ($i % 2 ? $table_color_1 : $table_color_2);   
$i++;   

$name = $spell['name']; 
$words = $spell['words']; 
$mana = $spell['mana']; 
$lvl = $spell['lvl']; 
$array[$j] = "$name,$words,$mana,$lvl"; 
$j = $j + 1; 

} 

$manual = 1; 
foreach($array as $var) {   
    $manual = $manual + 1; 
} 

for ($k = 0; $k <= $manual; $k++) { 
if ($k == 0) 
{ 
    $get = $array[0]; 
}else{ 
    $get = "$get,$array[$k]"; 
} 


} 

$show = explode(",", $get); 

$phantomVar = 0; 
for ($ForShow = 0; $ForShow <= $manual; $ForShow++) { 
    $showp1 = $phantomVar + 1; 
    $showp2 = $phantomVar + 2; 
    $showp3 = $phantomVar + 3; 

    echo '   
    <tr bgcolor="'.$color.'" id="spell_list">   
        <td style="font-size: 10pt;">'.$show[$phantomVar][.'</td>   
        <td style="font-size: 10pt;">'.$show[$showp1].'</td>   
        <td style="font-size: 10pt;">'.$show[$showp2].'</td>   
        <td style="font-size: 10pt;">'.$show[$showp3].'</td>   
    </tr>';   
    $phantomVar = $phantomVar + 4; 
} 


}   
else{alert('Error while loading spells. Directory address must be correct.');}   
?>   
</table>

Try that
 
I don't know if it still has the object issue but.....

Below code was not made by me (posted by a user from php.net):

PHP:
function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

To use:
$arr = array_sort($myarr, 'name', SORT_DESC) // SORT_ASC is default (that's the one you want)

So for your case:
$spells = array_sort($spells, 'name')
 
@Colandus
I am not sure what to do with that php code, should I add it somewhere? in that case where?
PHP:
function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}
 
Okay, ehm I am not sure if I made it right but:
PHP:
<style type="text/css">
td {
    color: white;
    height: 20px;
    color: #666;
}

</style>
<?php 
function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}  

if(!defined('BASEPATH')) exit('No direct script access allowed'); 

$DIR = 'C:/tibia/panchira/data/spells/'; 
     
if(is_dir($DIR)) { 
    $spells = simplexml_load_file($DIR.'spells.xml'); 
	$table_color_1 = 'black';
	$table_color_2 = '#080808';
?> 
<h2>Spells</h2>
<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%">
<tr  bgcolor="#101010 ">
<th style="color: #FF4500">Spell Name</th>
<th style="color: #FF4500">Words</th>
<th style="color: #FF4500">Mana Cost</th>
<th style="color: #FF4500">Level Required</th>
</tr>

<?php
$i = 1;
foreach($spells as $spell) {
$color = ($i % 2 ? $table_color_1 : $table_color_2);
$i++;
	if ($spell['words'] > null) {
	$spelln = array_sort($spell, 'name');
    echo '
	<tr bgcolor="'.$color.'" id="spell_list">
		<td style="font-size: 10pt;">'.$spelln.'</td>
		<td style="font-size: 10pt;">'.$spell['words'].'</td>
		<td style="font-size: 10pt;">'.$spell['mana'].'</td>
		<td style="font-size: 10pt;">'.$spell['lvl'].'</td>
	</tr>';
	}
}
}
else{alert('Error loading spells. Directory address must be correct.');}
?>
</table>

Now it replace all Spell Names with: Array :D
Did I understand you wrong or something?
 
try this... if it dont work show me error (if there be any)
PHP:
<style type="text/css">
td {
    color: white;
    height: 20px;
    color: #666;
}

</style>
<?php 
function array_sort($array, $on, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $on) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}  

if(!defined('BASEPATH')) exit('No direct script access allowed'); 

$DIR = 'C:/tibia/panchira/data/spells/'; 
     
if(is_dir($DIR)) { 
    $spells = simplexml_load_file($DIR.'spells.xml'); 
    $table_color_1 = 'black';
    $table_color_2 = '#080808';
?> 
<h2>Spells</h2>
<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%">
<tr  bgcolor="#101010 ">
<th style="color: #FF4500">Spell Name</th>
<th style="color: #FF4500">Words</th>
<th style="color: #FF4500">Mana Cost</th>
<th style="color: #FF4500">Level Required</th>
</tr>

<?php
$i = 1;
$spells = array_sort($spells, 'name');
foreach($spells as $spell) {
$color = ($i % 2 ? $table_color_1 : $table_color_2);
$i++;
    if ($spell['words'] > null) {
    echo '
    <tr bgcolor="'.$color.'" id="spell_list">
        <td style="font-size: 10pt;">'.$spelln.'</td>
        <td style="font-size: 10pt;">'.$spell['words'].'</td>
        <td style="font-size: 10pt;">'.$spell['mana'].'</td>
        <td style="font-size: 10pt;">'.$spell['lvl'].'</td>
    </tr>';
    }
}
}
else{alert('Error loading spells. Directory address must be correct.');}
?>
</table>
 
PHP:
<style type="text/css">
td {
    color: white;
    height: 20px;
    color: #666;
}

</style>
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

	$DIR = 'D:/IldeadfishlServer/ots/data/spells/';

	if(is_dir($DIR))
	{
		$spells = array();
		$table_color_1 = 'black';
		$table_color_2 = '#080808';
		$DOM = new DomDocument($DIR.'spells.xml');
		if($DOM->load($DIR.'spells.xml'))
		{
			foreach($DOM->getElementsByTagName('instant') as $spell)
				$spells[$spell->getAttribute('name')] = array(
					"words" => $spell->getAttribute('words'),
					"mana" => $spell->getAttribute('mana'),
					"lvl" => $spell->getAttribute('lvl')
				);
			foreach($DOM->getElementsByTagName('conjure') as $spell)
				$spells[$spell->getAttribute('name')] = array(
					"words" => $spell->getAttribute('words'),
					"mana" => $spell->getAttribute('mana'),
					"lvl" => $spell->getAttribute('lvl')
				);
?>
			<h2>Spells</h2>
			<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%">
			<tr  bgcolor="#101010 ">
			<th style="color: #FF4500">Spell Name</th>
			<th style="color: #FF4500">Words</th>
			<th style="color: #FF4500">Mana Cost</th>
			<th style="color: #FF4500">Level Required</th>
			</tr>

			<?php
			$i = 0;
			asort($spells);
			foreach($spells as $spell)
			{
				$color = ($i % 2 ? $table_color_1 : $table_color_2);
				$i++;
					if($spell['words'] > null)
					{
					echo '
					<tr bgcolor="'.$color.'" id="spell_list">
						<td style="font-size: 10pt;">'.$i.'</td>
						<td style="font-size: 10pt;">'.$spell['words'].'</td>
						<td style="font-size: 10pt;">'.$spell['mana'].'</td>
						<td style="font-size: 10pt;">'.$spell['lvl'].'</td>
					</tr>';
					}
			}
		}
		else
			alert('Error loading spells. Directory address must be correct.');
	}
	else
		alert('Error loading spells. Directory address must be correct.');
	?>
</table>

sup.

you better export this $spells array and save it instead of looping everytime.

You can also try using your way of parsing the XML, but change the $spells = array_sort()~ to asort($spells).
 
Evul <3
Thanks but now does another problem show up <_<

The spells name are replaced by numbers :D (1-103) Insted of their names.

I just noitice that the "Words" are sorted :p I think
 
PHP:
<style type="text/css">
td {
    color: white;
    height: 20px;
    color: #666;
}

</style>
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

	$DIR = 'D:/IldeadfishlServer/ots/data/spells/';

	function altsort($table, $field, $reverse = false)
	{
		$hash = array();
		$records = array();

		foreach($table as $record)
			$hash[$record[$field]] = $record;

		($reverse) ? krsort($hash) : ksort($hash);

		foreach($hash as $record)
			$records[] = $record;

		return $records;
	}

	if(is_dir($DIR))
	{
		$spells = array();
		$table_color_1 = 'black';
		$table_color_2 = '#080808';
		$DOM = new DomDocument($DIR.'spells.xml');
		if($DOM->load($DIR.'spells.xml'))
		{
			foreach($DOM->getElementsByTagName('instant') as $spell)
				$spells[] = array(
					"name" => $spell->getAttribute('name'),
					"words" => $spell->getAttribute('words'),
					"mana" => $spell->getAttribute('mana'),
					"lvl" => $spell->getAttribute('lvl')
				);
			foreach($DOM->getElementsByTagName('conjure') as $spell)
				$spells[] = array(
					"name" => $spell->getAttribute('name'),
					"words" => $spell->getAttribute('words'),
					"mana" => $spell->getAttribute('mana'),
					"lvl" => $spell->getAttribute('lvl')
				);
?>
			<h2>Spells</h2>
			<table style= border="0px" cellspacing="0px" cellpadding="4px" width="100%">
			<tr  bgcolor="#101010 ">
			<th style="color: #FF4500">Spell Name</th>
			<th style="color: #FF4500">Words</th>
			<th style="color: #FF4500">Mana Cost</th>
			<th style="color: #FF4500">Level Required</th>
			</tr>

			<?php
			$i = 0;
			$spells = altsort($spells, "name");
			foreach($spells as $spell)
			{
				$color = ($i % 2 ? $table_color_1 : $table_color_2);
				$i++;
					if($spell['words'] > null)
					{
					echo '
					<tr bgcolor="'.$color.'" id="spell_list">
						<td style="font-size: 10pt;">'.$spell['name'].'</td>
						<td style="font-size: 10pt;">'.$spell['words'].'</td>
						<td style="font-size: 10pt;">'.$spell['mana'].'</td>
						<td style="font-size: 10pt;">'.$spell['lvl'].'</td>
					</tr>';
					}
			}
		}
		else
			alert('Error loading spells. Directory address must be correct.');
	}
	else
		alert('Error loading spells. Directory address must be correct.');
	?>
</table>

You can sort by anything.
altsort(table, by what, reverse order?)
 
Last edited:
I was in need for such function last days, and I found something interesting. There's simpler way to do this in php. Just in case someone wants to know:
Code:
function compare($a, $b) {
	return $a['name'] > $b['name'] ? -1 : 1;
}
usort($spells, 'compare');

Whole philosophy :p
 
Back
Top