按数字值排序文本数据

I have mock fantasy football form that creates data in an external text file and I have everything working, I have just run into one problem. Can someone show me how I can sort the players by their number (largest on top) and then highlight (in red) the whole row of the player that has had the most points?

Here is my form file:

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>

<title>Fantasy Football</title>
</head>
<body>
<form action="team.php" method="POST">
<table border="1">
<tr><td>Player Name</td><td><input type="text" name="name"</td></tr>
<tr><td>Position</td><td><input type="text" name="position"</td></tr>
<tr><td>Number</td><td><input type="text" name="number"</td></tr>
<tr><td>Team</td><td><input type="text" name="team"</td></tr>
<tr><td>Points per game</td><td><input type="text" name="points"</td></tr>
<tr><td colspan="2" align="center"><input type="submit"></td></tr>
</table>
</form>
</body>
</html>

And here is the return data:

<?php
$name = $_POST['name'];
$team = $_POST['team'];
$number = $_POST['number'];
$position = $_POST['position'];
$points = $_POST['points'];
$DOC_ROOT = $_SERVER['DOCUMENT_ROOT'];

@ $fp = fopen("$DOC_ROOT/../php/football.txt","ab");

if(!$fp) {
echo 'Error: Cannot open file.';
exit;
} 

fwrite($fp, $name."|".$team."|".$number."|".$position."|".$points."
");
?>

<?php
$DOC_ROOT = $_SERVER['DOCUMENT_ROOT'];
$players = file("$DOC_ROOT/../php/football.txt");
echo "<table border='2'>";
echo "<tr> <td>Name</td> <td>Team</td> <td>number</td> <td>Position</td> <td>Points</td> </tr>";
for($i = 0; $i < sizeof($players); $i++) {
list($name,$team,$number,$position,$points) = explode('|', $players[$i]);
 echo '<tr><td>'.$name.'</td><td>'.$team.'</td><td>'.$number.'</td>
 <td>'.$position.'</td><td>'.$points.'</td></tr>';
 }
 echo '</table>';
 ?>

Not really good at inserting bits an pieces of code, so if you could, can you tell me exactly where to put whatever you can give me?

Update:

Is this where I needed to put everything? The way it is now, when I submit my form I don't see anything! Obviously I did something wrong, so I'm open to suggestions!

    <?php
function sort_player_array( $array, $key, $asc = true) {
$result = array();

$values = array();
foreach ($array as $id => $value) {
    $values[$id] = isset($value[$key]) ? $value[$key] : '';
}

if ($asc) {
    asort($values);
}
else {
arsort($values);
}

foreach ($values as $key => $value) {
    $result[$key] = $array[$key];
}

return $result;
}
?>
<?php
$name = $_POST['name'];
$team = $_POST['team'];
$number = $_POST['number'];
$position = $_POST['position'];
$points = $_POST['points'];
$DOC_ROOT = $_SERVER['DOCUMENT_ROOT'];

@ $fp = fopen("$DOC_ROOT/../php/football.txt","ab");

if(!$fp) {
echo 'Error: Cannot open file.';
exit;
}
?>
<?php
fwrite($fp, $name."|".$team."|".$number."|".$position."|".$points."
");

$players = file("$DOC_ROOT/../php/football.txt");
$player_array = array();

foreach($players AS $player)
{
list($name,$team,$number,$position,$points) = explode('|', $players[$i]);
$player_array[] = array('name'     => $name,
                        'number'   => $number,
                        'position' => $position,
                        'points'   => $points,
);
}

$sorted_players = sort_player_array($player_array, 'number', true);

foreach( $sorted_players AS $player )
{
echo '<tr><td>'.$player[name].'</td><td>'.$player[team].'</td><td>'
.$player[number].'</td><td>'.$player[position].'</td><td>'.$player[points].'</td></tr>';
} ?>

Seems like you need some kind of sorting algorithm function for $players. There are different kinds, and you can find many by googling "sorting algorithms", or you can write one yourself if you feel like "reinventing the wheel". Doing one yourself does make for good practice/fun :D

Here's a link to a wiki on bubble sort, probably the most common basic sorting and would help in your situation.

Prepare an array $playerData, which contains all player records and use their numbers as key. Then use ksort():

ksort( $playerData );

While preparing the $playerData, keep a variable $maxPoints and $mapPointsPlayerNumber and check each player's data against these values. Update them if current player's point is higher than $maxPoints.

first of all you are programming in PHP, use COUNT() instead of SIZEOF(). sizeof() is an alias of count() and might be removed as php evolves.

We need a function for sorting the array:

function sort_player_array( $array, $key, $asc = true) {
    $result = array();

    $values = array();
    foreach ($array as $id => $value) {
        $values[$id] = isset($value[$key]) ? $value[$key] : '';
    }

    if ($asc) {
        asort($values);
    }
    else {
    arsort($values);
    }

    foreach ($values as $key => $value) {
        $result[$key] = $array[$key];
    }

    return $result;
}

next build an array with php holding your data, then sort it .

$players = file("$DOC_ROOT/../php/football.txt");
$player_array = array();

foreach($players AS $player)
{
    list($name,$team,$number,$position,$points) = explode('|', $players[$i]);
    $player_array[] = array('name'     => $name,
                            'number'   => $number,
                            'position' => $position,
                            'points'   => $points,
    );
}

We sort the array as you requested by number, but any key is possible. also you can set ASC or DESC with the third variable

$sorted_players = sort_player_array($player_array, 'number', true);

foreach( $sorted_players AS $player )
{
    echo '<tr><td>'.$player[name].'</td><td>'.$player[team].'</td><td>'.$player[number].'</td><td>'.$player[position].'</td><td>'.$player[points].'</td></tr>';
}

I think you should be able to create arrays with the list function. Something like this:

    //set array variables
    for($i = 0; $i < sizeof($players); $i++) {
    list($name[],$team[],$number[],$position[],$points[]) = explode('|', $players[$i]);
     }
//sort all arrays by the number, in descending order
array_multisort($number, $position, $name, $team, $points, SORT_DESC);
//set the highest points to mostPoints variable
var mostPoints = max($points);
    //Output table rows
    for($i = 0; $i < sizeof($players); $i++) {
    if($points[$i]==mostPoints){
        //red background
        echo '<tr style="background:#F44">';
}else{
        echo '<tr>';
}
echo '<td>'.$name[$i].'</td><td>'.$team[$i].'</td><td>'.$number[$i].'</td><td>'.$position[$i].'</td><td>'.$points[$i].'</td></tr>';
    }

I haven't tested this, so there may be a few things in there that I've missed, but it should work. See Array Multisort, and max functions. It may be better to assign a class to the red table row, instead of a style; that way you can alter the td tags; I think that's more browser-friendly. The CSS for that would be something like .redRow td {background:#F44} if you gave the tr the redRow class.