Forum Moderators: coopster

Message Too Old, No Replies

Mutidimensional Array - I think?

         

phpWhat

10:21 am on Apr 13, 2011 (gmt 0)

10+ Year Member



Hi

Not too sure what I need to do so any help would be welcome.

I an running a snooker website and I want to have player profiles. A player has:
name,team,gamesplayed,gameswon,gameslost,average

There are many players. I want to be able to display them in a table and to be able to sort them by any of the values.

Is this a multidimensional array? I need to fill the array up in a loop, sort it and then display it.

I would appreciate any help here, thanks

mbabuskov

10:34 am on Apr 13, 2011 (gmt 0)

10+ Year Member



You can use multidimensional arrays. For sorting on any column, take a look at array_multisort PHP function.

rocknbil

4:02 pm on Apr 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard phphWhat . . .

name,team,gamesplayed,gameswon,gameslost,average


This is in mySQL I am presuming? Arrays are not needed, if this is the case. Too often developers program for functions that are already inherent in mySQL.

I want to be able to display them in a table and to be able to sort them by any of the values.



This (or something like it) will give you a table with clickable links at the head to re-sort the results by the field. I've left a lot of stuff out for simplicity's sake, and this has NOT been tested and may contain errors - but it's a starting point.


// 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;


Of course, if that works you'd want to add an option to sort in ascending and descending order, and will want to add pagination, but it's a decent starting point.

phpWhat

8:51 am on Apr 14, 2011 (gmt 0)

10+ Year Member



thanks guys, I'll give that a try.