Jump to content

Recommended Posts

Hello

 

I have very complicated table and problem with sorting it. It's a table of basketball league and I want to sort it by points and then by games between teams having same number of points. Can anybody help me how to do this?

 

<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=1 width=550 align=center> 
<?  

function multi_sort($array, $akey) 
{  
  function compare($a, $b) 
  { 
     global $key; 
     return strcmp($a[$key], $b[$key]); 
  } 
  usort($array, "compare"); 
  return $array; 
} 

  function tab_plk_duza() {  
  static $color; 
  if($color == "#f6f6f6") { 
  $color = "#efefef"; 
  } else { 
  $color = "#f6f6f6"; 
  } 
  return($color); 
  } 
   
$db = mysql_connect("localhost", "XXX", "XXX"); 
mysql_select_db("XXX", $db); 

$miejsce = 0; 


$sql = "SELECT klub FROM klub WHERE poddzial=1 ORDER BY klub"; 
$wynik = mysql_query($sql); 
$id=Array(); 
$i=0; 

while ($row = mysql_fetch_array($wynik)) { 

    $sql="SELECT SUM(tabele.punkty) AS punkty, (SUM(zdobyte)/SUM(stracone)) AS bilans FROM klub,tabele WHERE klub.poddzial=1 AND tabele.rozgrywki=1 AND tabele.sezon=2007 AND klub.klub=".$row["klub"]." AND tabele.klub=".$row["klub"]." ORDER BY punkty DESC"; 
    $res=mysql_fetch_array(mysql_query($sql)); 
    $id[$i]['bilans'] = $res["bilans"]; 
    $id[$i]['punkty'] = $res["punkty"]; 
    $id[$i]['klub'] = $row["klub"]; 
    $i++; 
} 


/* 
foreach($id as $res) 
     $sortAux[] = $res['bilans']; 
array_multisort($sortAux, SORT_DESC, $id); 
*/ 

$i=0; 
foreach ($id as $res2) { 
$matrix[0][$i]=$res2['punkty']; 
$matrix[1][$i]=$res2['bilans']; 
$matrix[2][$i]=$res2['klub']; 
$i++; 
} 

array_multisort($matrix[0], SORT_DESC, SORT_NUMERIC,$matrix[1], SORT_DESC, SORT_NUMERIC,$matrix[2], SORT_DESC, SORT_NUMERIC); 

//print_r($matrix); 


$i=0; 
$id2=Array(); 

foreach ($id as $res) { 
       $id2[$i]['bilans']=$matrix[1][$i]; 
       $id2[$i]['punkty']=$matrix[0][$i]; 
       $id2[$i]['klub']=$matrix[2][$i]; 
       $i++; 
}        


foreach ($id2 as $tab)  
{ 


$sql = "SELECT MAX(zdobyte) AS max_zdobyte, MAX(stracone) AS max_stracone, SUM(p+w) AS mecze, SUM(punkty) AS punkty, SUM(zdobyte) AS zdobyte, SUM(stracone) AS stracone, SUM(w) AS w, SUM(p) AS p, (SUM(zdobyte)/SUM(stracone)) AS bilans, AVG(zdobyte) AS srednia_zdobyte, AVG(stracone) AS srednia_stracone FROM tabele WHERE rozgrywki=1 AND sezon=2007 AND klub='".$tab['klub']."'"; 
        $wynik = mysql_query($sql); 
         
         
        while ($r = mysql_fetch_array($wynik)) { 
         
                $mecze = $r["mecze"]; 
                $punkty = $r["punkty"]; 
                $w = $r["w"]; 
                $p = $r["p"]; 
                $srednia_zdobyte = $r["srednia_zdobyte"]; 
                $srednia_stracone = $r["srednia_stracone"]; 
                $max_zdobyte = $r["max_zdobyte"]; 
                $max_stracone = $r["max_stracone"]; 
                $stracone = $r["stracone"]; 
                $zdobyte = $r["zdobyte"]; 
                $bilans = $r["bilans"]; 

                $srednia_zdobyte = substr($srednia_zdobyte, 0,4); 
                $srednia_stracone = substr($srednia_stracone, 0,4); 
                 
                 
                $q = "SELECT nklub FROM klub WHERE klub='".$tab['klub']."'"; 
                $r = mysql_query($q); 
                   while ($row = mysql_fetch_array($r)) { 
                       $zespol = $row["nklub"];  
                       //$klub  = $row["klub"];  
         
                       //$zespol = ereg_replace("$klub", "$nklub", $zespol); 
                   } 
        } 

$bgcolor = tab_plk_duza(); 

print "<TR><TD width=20 align=right bgcolor=$bgcolor><font id=tabela>"; 
print ++$miejsce.".</font></TD> 
<TD width=180 bgcolor=$bgcolor><A HREF=/klub/".$tab['klub']." class=b2>$zespol</a></TD> 
<TD align=center width=25 bgcolor=$bgcolor><font id=tabela>$mecze</FONT></TD> 
<TD align=center width=25 bgcolor=$bgcolor><font id=tabela>$w</FONT></TD> 
<TD align=center width=25 bgcolor=$bgcolor><font id=tabela>$p</FONT></TD> 
<TD align=center width=25 bgcolor=$bgcolor><font id=tabela>".$tab['punkty']."</FONT></TD> 
<TD align=right width=30 bgcolor=$bgcolor><font id=tabela>$zdobyte</FONT></TD> 
<TD align=center width=5 bgcolor=$bgcolor><font id=tabela>:</FONT></TD> 
<TD align=left width=30 bgcolor=$bgcolor><font id=tabela>$stracone</FONT></TD> 
<TD align=center width=40 bgcolor=$bgcolor><font id=tabela>".$tab['bilans']."</FONT></TD> 
<TD align=center width=40 bgcolor=$bgcolor><font id=tabela>$srednia_zdobyte</FONT></TD> 
<TD align=center width=40 bgcolor=$bgcolor><font id=tabela>$srednia_stracone</FONT></TD> 
<TD align=center width=40 bgcolor=$bgcolor><font id=tabela>$max_zdobyte</FONT></TD> 
<TD align=center width=40 bgcolor=$bgcolor><font id=tabela>$max_stracone</FONT></TD> 
</TR>"; 


} 


print "</table>"; 

?> 
<BR><font id=tabela>Tabela: rozegrane mecze; zwycięstwa; porażki; zdobyte punkty; kosze zdobyte; kosze stracone; stosunek koszy zdobytych do straconych; ¶rednia ilo¶ć zdobywanych koszy; ¶rednia ilo¶ć traconych koszy; najwięcej rzuconych koszy; najwięcej straconych koszy.</FONT> 

Link to comment
https://forums.phpfreaks.com/topic/97434-problem-with-sorting/
Share on other sites

I think it should be somewhere here

 

order by punkty then check if two (or more) teams (klub in polish) have the same number of points (punkty), search these teams id in database and check games between them and points scored in these games (zdobyte)

 

while ($row = mysql_fetch_array($wynik)) { 

    $sql="SELECT SUM(tabele.punkty) AS punkty, (SUM(zdobyte)/SUM(stracone)) AS bilans FROM klub,tabele WHERE klub.poddzial=1 AND tabele.rozgrywki=1 AND tabele.sezon=2007 AND klub.klub=".$row["klub"]." AND tabele.klub=".$row["klub"]." ORDER BY punkty DESC"; 
    $res=mysql_fetch_array(mysql_query($sql)); 
    $id[$i]['bilans'] = $res["bilans"]; 
    $id[$i]['punkty'] = $res["punkty"]; 
    $id[$i]['klub'] = $row["klub"]; 
    $i++; 
} 

Link to comment
https://forums.phpfreaks.com/topic/97434-problem-with-sorting/#findComment-498753
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.