Php 按数值对文本数据排序
我有一个模拟幻想足球的表单,它在一个外部文本文件中创建数据,我的一切都正常工作,我只是遇到了一个问题。有人能告诉我如何根据球员人数(顶部最大)对球员进行排序,然后突出显示(红色)得分最多的球员的整行吗 这是我的表格文件:Php 按数值对文本数据排序,php,forms,sorting,Php,Forms,Sorting,我有一个模拟幻想足球的表单,它在一个外部文本文件中创建数据,我的一切都正常工作,我只是遇到了一个问题。有人能告诉我如何根据球员人数(顶部最大)对球员进行排序,然后突出显示(红色)得分最多的球员的整行吗 这是我的表格文件: <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title>Fantasy Football</title> &
<!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>
梦幻足球
玩家名称似乎需要某种排序算法函数来处理$players
。有很多种,你可以通过谷歌搜索“排序算法”找到很多,如果你想“重新发明轮子”,你也可以自己写一个。自己做一件事确实有助于良好的练习/乐趣:D
到一个关于冒泡排序的wiki,这可能是最常见的基本排序,在您的情况下会有所帮助。准备一个数组$playerData
,其中包含所有玩家记录并将其数字用作键。然后使用:
在准备$playerData
时,保留一个变量$maxPoints和$mapPointsPlayerNumber,并对照这些值检查每个玩家的数据。如果当前玩家的点数高于$maxPoints,请更新它们。首先,在使用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>';
}
$sorted\u players=sort\u player\u数组($player\u数组,'number',true);
foreach($player作为$player排序)
{
回显“.$player[姓名]。$player[球队]。$player[人数]。$player[位置]。$player[积分]。”;
}
我认为您应该能够使用list函数创建数组。大概是这样的:
//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=0;$i
我还没有测试过这个,所以可能有一些东西我错过了,但它应该可以工作。请参阅,以及功能。最好给红色表格行指定一个类,而不是样式;这样,您可以更改td
标记;我认为这对浏览器更友好。如果给tr
类指定redRow,那么它的CSS类似于.redRow td{background:#F44}
$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>';
}
//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>';
}