Forum › PHP / SQL › Topic

Een nieuwe topic in het forum PHP / SQL plaatsen Reageren op dit topic Forum:

[PHP/SQL] Competitiestand ordenen

Beste,

kan iemand mij vertellen hoe ik dit kan sorteren met multi level arrays?

Winnen = 3 punten
Gelijk = 1 punt

Meeste punten is nummer 1, indien gelijk aantal punten, dan krijgt het team met het beste doelsaldo voorrang (doelsaldo is aantal gescoord – aantal tegen). Indien ook doelsaldo gelijk (bijvoorbeeld +11) dan aantal gescoord voorrang, indien dat ook gelijk is, dan onderling resultaat (ik weet niet of dat lastig is, maar als ze nog niet tegen elkaar gespeeld hebben zoals in het begin van een seizoen voorkomt, dan gewoon op alfabetische volgorde).


Code tonen/verbergenCodeDeze code in een nieuw vensterDeze code in een tekstveldDeze code in een zip file downloaden
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once('config.php');

// create the arrays
$query mysql_query('SELECT * FROM site_teams
                      ORDER BY club'
) or die(mysql_error());
while(
$fetch mysql_fetch_assoc($query))
{
    if((
$fetch['voor'] == NULL || $fetch['voor'] <= 0) || ($fetch['against'] == NULL || $fetch['against'] <= 0))
    {
        
$saldo 0;    
    }
    else
    {
        
$saldo = ($fetch['voor'] - $fetch['against']);
    }
    
    
$list[] = array(
        
'club' => $fetch['club'],
        
'games' => $fetch['games'],
        
'win' => $fetch['win'],
        
'loss' => $fetch['loss'],
        
'draw' => $fetch['draw'],
        
'points' => $fetch['points'],
        
'voor' => $fetch['voor'],
        
'against' => $fetch['against'],
        
'saldo' => $saldo
    
);    
}

// obtain a list of columns
foreach ($list as $value => $row) {
    
$club[$value]  = $row['club'];
    
$games[$value] = $row['games'];
    
$win[$value]  = $row['win'];
    
$loss[$value] = $row['loss'];
    
$draw[$value]  = $row['draw'];
    
$points[$value]  = $row['points'];
    
$voor[$value]  = $row['voor'];
    
$against[$value]  = $row['against'];
    
$saldo[$value]  = $row['saldo'];
}

// sort the data with volume descending, edition ascending
// add $data as the last parameter, to sort by the common key
array_multisort($pointsSORT_DESC$saldoSORT_DESC$list);
?>

<table width="30%" style="margin-top:25px">
    <thead style="text-align:left">
        <tr>
            <th>Club</th>
            <th>Games</th>
            <th>Gewonnen</th>
            <th>Verloren</th>
            <th>Gelijk</th>
            <th>Punten</th>
            <th>Voor</th>
            <th>Against</th>
            <th>Saldo</th>
        </tr>
    </thead>
    <tbody>    
        <?php
        
foreach($list as $value => $row)
        {
            echo 
'<r>
                    <td>'
.$row['club'].'</td>
                    <td>'
.$row['games'].'</td>
                    <td>'
.$row['win'].'</td>
                    <td>'
.$row['loss'].'</td>
                    <td>'
.$row['draw'].'</td>
                    <td>'
.$row['points'].'</td>
                    <td>'
.$row['voor'].'</td>
                    <td>'
.$row['against'].'</td>
                    <td>'
.$row['saldo'].'</td>
                  </tr>'
;
        }
        
?>
    </tbody>
</table>



Stuur een prive bericht