Jump to content

**SOLVED** ranking table...


brown2005

Recommended Posts

Right, what I want to do is create three lists..... in ranking order... BASED ON THE NUMBER OF THEM, I.E. IF THERE IS 5 RICHARDS AND 3 EMILYS....

1. FIRST NAMES - BOTH MALES AND FEMALES

1 - RICHARD - 5
2 - EMILY - 3

2. FIRST NAMES - MALES ONLY

1 - RICHARD - 5

3. FIRST NAMES - FEMALES ONLY

1 - EMILY - 3

the table is members

and has the following fields

members_firstname
members_sex
Link to comment
https://forums.phpfreaks.com/topic/11054-solved-ranking-table/
Share on other sites

Something like this maybe

[code]function rankNames ($sex='') {
    $sql = "SELECT firstname, COUNT(*) as total
            FROM members
            WHERE sex LIKE '$sex%'
            GROUP BY firstname
            ORDER BY total DESC";
    $res = mysql_query($sql) or die(mysql_error());

    switch ($sex) {
        case 'M':
            echo '<br>MALES ONLY<br>';
            break;
        case 'F':
            echo '<br>FEMALES ONLY<br>';
            break;
        default:            
            echo '<br>MALE and FEMALE<br>';
            break;
    }
    $rank=1;
    while (list($name, $tot) = mysql_fetch_row($res)) {
        echo "$rank $name $tot<br>";
        ++$rank;
    }
}

rankNames();
rankNames('M');
rankNames('F');[/code]
Link to comment
https://forums.phpfreaks.com/topic/11054-solved-ranking-table/#findComment-41313
Share on other sites

Try

[code]$ranks = array();

$sql = "SELECT sex, firstname, COUNT(*) as total
        FROM members
        GROUP BY sex,firstname
        ORDER BY total DESC";
$res = mysql_query($sql) or die(mysql_error());

$rank = 0;
while (list($sex, $name, $tot) = mysql_fetch_row($res)) {
    $ranks['BOTH'][] = array($name,$tot);
    $ranks[$sex][] = array($name,$tot);
    $rank++;
}

echo "<table border='1'>";
echo "<tr>
    <td>RANK</td>
    <td>ALL</td>
    <td>MALE</td>
    <td>FEMALE</td>
    </tr>";

for ($r=1; $r<=$rank; $r++) {
     echo "<tr>
         <td>$r</td>
         <td>{$ranks['BOTH'][$r-1][0]} - {$ranks['BOTH'][$r-1][1]}</td>
         <td>{$ranks['M'][$r-1][0]} - {$ranks['M'][$r-1][1]}</td>
         <td>{$ranks['F'][$r-1][0]} - {$ranks['F'][$r-1][1]}</td>
         </tr>";
}
echo "</table>";[/code]
Link to comment
https://forums.phpfreaks.com/topic/11054-solved-ranking-table/#findComment-41333
Share on other sites

Define styles for M/F cells. Save sex in the array as well as name and count and use to define the TD.class.

For the count, store number_format($tot,0);

[code]<STYLE type="text/css">
    TD.M {color: #8CAAE1}
    TD.F {color: #FFC0C0}
</STYLE>

<?php
include 'db.php';

$ranks = array(
    'BOTH' => array(),
    'M'    => array(),
    'F'    => array()
);

$sql = "SELECT sex, firstname, COUNT(*) as total
        FROM members
        GROUP BY sex,firstname
        ORDER BY total DESC";
$res = mysql_query($sql) or die(mysql_error());

$rank = 0;
while (list($sex, $name, $tot) = mysql_fetch_row($res)) {
    $ranks['BOTH'][] = array($name, number_format($tot, 0), $sex);
    $ranks[$sex][] = array($name, number_format($tot, 0), $sex);
    $rank++;
}

echo "<table border='1'>";
echo "<tr>
    <td>RANK</td>
    <td>ALL</td>
    <td>MALE</td>
    <td>FEMALE</td>
    </tr>";

for ($r=1; $r<=$rank; $r++) {
     echo "<tr>
         <td>$r</td>
         <td class='{$ranks['BOTH'][$r-1][2]}'>{$ranks['BOTH'][$r-1][0]} - {$ranks['BOTH'][$r-1][1]}</td>
         <td class='{$ranks['M'][$r-1][2]}'>{$ranks['M'][$r-1][0]} - {$ranks['M'][$r-1][1]}</td>
         <td class='{$ranks['F'][$r-1][2]}'>{$ranks['F'][$r-1][0]} - {$ranks['F'][$r-1][1]}</td>
         </tr>";
}
echo "</table>";
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/11054-solved-ranking-table/#findComment-41410
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.