douyoupingji7238 2011-07-11 20:36
浏览 127

按数字值排序文本数据

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>';
} ?>
  • 写回答

4条回答 默认 最新

  • doudou32012 2011-07-11 20:43
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大