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.

    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)