按数值排序文本数据


Sorting text data by number value

我有模拟梦幻足球表单,在外部文本文件中创建数据,我有一切工作,我刚刚遇到一个问题。有人能告诉我如何根据玩家的数量(最大的在上面)对玩家进行排序,然后突出显示(红色)得分最多的玩家的整排吗?

这是我的表单文件:
<!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>

返回数据:

<?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."'n");
?>
<?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>';
 ?>

我不太擅长插入代码,所以如果可以的话,你能告诉我你能给我的东西放在哪里吗?

更新:

我需要把所有东西都放在这里吗?现在的情况是,当我提交表单时,我什么也看不到!显然我做错了什么,所以我愿意接受建议!

    <?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."'n");
$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>';
} ?>

首先你是在PHP编程,使用COUNT()而不是SIZEOF()。Sizeof()是count()的别名,可能会随着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构建一个保存数据的数组,然后对其进行排序。

$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,
    );
}

我们按您要求的数字对数组进行排序,但任何键都是可能的。也可以用第三个变量

设置ASC或DESC
$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>';
}

看来你需要$players的某种排序算法函数。排序算法有不同的种类,你可以通过谷歌搜索"排序算法"找到很多,或者如果你想"重新发明轮子",你也可以自己写一个。自己做一件确实是很好的练习/乐趣:D

这是一个关于冒泡排序的wiki链接,可能是最常见的基本排序,对你的情况会有帮助。

准备一个数组$playerData,其中包含所有的球员记录,并使用他们的数字作为键。然后使用ksort():

ksort( $playerData );

在准备$playerData时,保持变量$maxPoints和$mapPointsPlayerNumber,并根据这些值检查每个玩家的数据。如果当前玩家的点数高于$maxPoints,则更新它们。

我认为你应该能够创建数组与列表函数。像这样:

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

我还没有测试过这个,所以可能有一些东西在那里,我错过了,但它应该工作。参见数组多排序和最大函数。最好是给红色的表行分配一个类,而不是一个样式;这样你就可以改变td标签;我认为这样对浏览器更友好。如果你给tr一个redRow类的话,它的CSS应该是.redRow td {background:#F44}