Forum Moderators: coopster
name,team,gamesplayed,gameswon,gameslost,average
I want to be able to display them in a table and to be able to sort them by any of the values.
// Assuming you've already made a connection to the DB.
$order=$results=$bg=null; //Squelch concatenation warnings
$thisScript = 'output-test.php'; // Name of this script, SELF has vulerabilities
// Define this as an array so we don't reveal database table names publicly
$fields = array(
'plr' => 'name',
'tm' => 'team',
'games' => 'gamesplayed',
'won' => 'gameswon',
'lost' => 'gameslost',
'avg' => 'average'
);
// If a link is clicked, otherwise, by name
$order = (isset($_GET['sort']) and array_key_exists($_GET['sort'],$fields))?$fields[$_GET['sort']]):'name';
$query = 'select name,team,gamesplayed,gameswon,gameslost,average from players order by $order asc";
$result = mysql_query($query) or die ("Could not execute query at line 14");
while ($row = mysql_fetch_array($result)) {
// alternate row colors
$bg = ($bg=='#c0c0c0')?'#fff':'#c0c0c0';
$results .= '<tr>';
foreach ($fields as $key => $value) {
$val = (! empty($row[$value]))?$row[$value]:'-';
$results .= "<td style=\"background:$bg\">$val</td>";
}
$results .= '</tr>';
}
// If results, output the header, if not, output . . .something. :-)
if ($results) {
// Two arrays is not the best solution, but is good enough for an example.
$heads = array(
'plr' => 'Player Name',
'tm' => 'Team',
'games' => 'Games Played',
'won' => 'Games Won',
'lost' => 'Games Lost',
'avg' => 'Average'
);
$output = '<table><tr>';
foreach ($heads as $key=>$value) {
$output .= "<th><a href=\"$thisScript?sort=$key\">$value</th>";
}
$output .= "</tr>
$results
</table>";
}
else { $output = "<p>There are no results to display.</p>"; }
echo $output;