how do i Php Sort ?

I am a bit new to PHP, I am trying to create away to sort kills,points,deaths,userid. I did try googleing but did not really get it.

Any help wold be appreciated.

text file to the code -> http://www.l3git.com/servers/test_2.txt

before the php code does its thing -> -> http://www.l3git.com/servers/users

what it looks like on an output -> http://www.l3git.com/servers/test_2.php

[php]

<? $i = 0; $taken = 0; $e = 0; $type = $_GET['type']; list($type,$step2,$step3,$step4,$step5) = split(";",$type); if($step2 < "0"){$step2 = "25";} if($step3 < "0"){$step3 = "0";} if($step2 > "200"){$step2 = "200"; echo '
Sorry the max is 200
';} $data = file_get_contents('users'); $replace = array('INSERT INTO Users VALUES','"Users" ("userId" BigInt PRIMARY KEY NOT NULL , "UserType" int NOT NULL , "TotalTime" int, "TotalVisits" int);','INSERT INTO WelcomeActions VALUES','INSERT INTO Characters VALUES','("UserId" BigInt NOT NULL ,"CharacterId" Binint NOT NULL ,"Name" varchar(50) NOT NULL ,"Level" int,"Class" int, "Kills" int, "Deaths" int, "Points" int, "TotalTime" int, "TotalVisits", PRIMARY KEY ','"userId"','"UserType" int NOT NULL , "TotalTime" int, "TotalVisits" int)'); $other_replace = array('BigInt','PRIMARY KEY','NOT NULL','( ',', ','("UserId","CharacterId") )','("UserId" bigint"Type" int"Repeats" int"Text" varchar(300)"Delay" int"Source" int)','COMMIT',')','(',"'",'BEGIN TRANSACTION'); $img_1 = str_replace($replace,'',$data); $img_2 = str_replace($other_replace,'',$img_1); list($nothing1, $users, $nothing2, $vips, $mbl) = split('CREATE TABLE', $img_2); if($type == "vips"){$result = explode(';',$vips);} elseif($type == "mbl"){$result = explode(';',$mbl);} elseif($type == "users"){$result = explode(';',$users);} elseif(($type <> "users") || ($type <> "vips") || ($type <> "mbl")){$result = explode(';',$users);} echo 'Users Found: '.count($result); ?>
       <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr>
          <td class="l3_name-title">UserName</td>
          <td class="l3_rank-title">Rank</td>
          <td class="l3_class-title">Class</td>
          <td class="l3_kills-title">Kills</td>
          <td class="l3_kia-title">Deaths</td>
          <td class="l3_points-title">Points</td>
          <td class="l3_time-title">Time</td>
          <td class="l3_visits-title">Visits</td>
        </tr>
       </table>
       <table cellpadding="0" cellspacing="0" border="0" width="100%">
      <?

  		  
	  foreach($result as $value){
		  list($UserId, $CharacterId, $Name, $Rank, $Class, $Kills, $Deaths, $Points, $TotalTime, $TotalVisits) = split(',',$value);

		  
		if(($Class == "NULL") || ($Rank == "-1") || ($TotalTime == "0")){$e++;}
		if ($i++-1>=$step3 && $taken++<$step2){
		  
		   if(($Class == "NULL") || ($Rank == "-1") || ($TotalTime == "0")){}
		    else{
		  
		   
		   ?>
             <tr>
               <td width="25" style="text-align:center; border-right:#000 thin solid;"><?=$i-1?></td>
               <td class="l3_name"><a href="#<?=$UserId?>/<?=$CharacterId?>"><?=$Name?></a></td>
               <td class="l3_rank"><?=$Rank?></td>
               <td class="l3_class"><?=$Class?></td>
               <td class="l3_kills"><?=$Kills?></td>
               <td class="l3_kia"><?=$Deaths?></td>
               <td class="l3_points"><?=$Points?></td>
               <td class="l3_time"><?=$TotalTime?></td>
               <td class="l3_visits"><?=$TotalVisits?></td>
             </tr>
           <?
		    if($taken == $step3){break;}
			}
	  }
	  }
	 
		  echo '</table>';
		   echo ' | Failed players:'.$e;

[/php]

Thank you for your time.

Have a look at the different array functions at php.net or search for php array sorting.

I tryed the sort() and other things played with them for 3h could not get any of them working.

If this information is coming from a query, just tak ORDER BY to the end. It would be impracticle to sort by all those things at once, but what you can do is use a default sort order, then use links to change the sort order.

Can you tell us a little more about how you want the sort to work.

Are you looking to simply sort the values in the same way every time or interactively re-sort the data (by clicking on the table heads or similar)?

Sorting multidimensional arrays by subvals is tricky and not inherently built into php, but it can be done.

I would like to be able to click and sort by. Or just one of them like $Points
[php]list($UserId, $CharacterId, $Name, $Rank, $Class, $Kills, $Deaths, $Points, $TotalTime, $TotalVisits)[/php]

The best way that I know to sort your list will be something like this:

Create an array with your list as keys=>values. This could be done with something like:[php]$result = mysql_query('SELECT * FROM Characters');
while($rows[] = mysql_fetch_assoc($result));[/php]

Here is a function that will sort a multidimensional array by one of its keys. I put an optional parameter to allow for ascending or descending sorts (‘ASC’ or ‘DESC’ - it defaults to ascending)[php]function sub_sort($data,$sortkey,$dir=‘ASC’)
{
foreach($data as $key=>$val)
{
$data2[$key] = strtolower($val[$sortkey]);
}
if($dir != ‘DESC’) asort($data2);
else arsort($data2);
foreach($data2 as $key=>$val)
{
$data3[] = $data[$key];
}
return $data3;
}
[/php]

To use it you would do something like this:[php]$data = sub_sort($rows,‘Kills’,‘DESC’);[/php]

Putting all of this together, you could do something like this:[php]<?php
function sub_sort($data,$sortkey,$dir=‘ASC’)
{
foreach($data as $key=>$val)
{
$data2[$key] = strtolower($val[$sortkey]);
}
if($dir != ‘DESC’) asort($data2);
else arsort($data2);
foreach($data2 as $key=>$val)
{
$data3[] = $data[$key];
}
return $data3;
}

$result = mysql_query('SELECT * FROM Characters');
while($rows[] = mysql_fetch_assoc($result));

$data = sub_sort($rows,‘Kills’,‘DESC’);

$rows2 = array_slice($data,0,100);

echo ‘

’;
print_r($rows2);
?>[/php]

You would need to include your db connect string. This would fetch all of the rows of your database into the array $rows. It then sorts (using our sub_sort function) the array by Kills (Descending) and assigns the newly sorted array to $data. Finally it sets $rows2 to the first 100 rows of the sorted array (to make it more manageable). It the uses php’s print_r to display the contents of the sorted list.

I hope this makes sense. Let me know if you need some help implementing it or have any questions.

jay

Thank all. I will see what i can do with that info. Thx for all your help guys.

No problem,

Let me know if you run into any issues.

You could tackle this issue several different ways but by populating an array and then manipulating it as needed, you should be able to use javascript to create a more dynamic table. It will use more memory, but should be quicker and put less of a load on your server and database.

@malasho, your suggestion uses dangerous, obsolete code that has been completely removed from Php.

Sponsor our Newsletter | Privacy Policy | Terms of Service