【问题标题】:Sort the $_POST variables对 $_POST 变量进行排序
【发布时间】:2011-02-25 19:32:56
【问题描述】:

对你们来说可能很容易。我正在尝试对表单发送的 $_POST 变量进行排序,并在 mysql 中更新排序结果。我不知道该怎么做,感谢任何人都可以帮助我。

我的 main.php

//I have a loop here. (omitted)
//$k will be increased by 1 every time the loop starts, so I will know the total times of the loops
//the form will be submitted to update.php


echo "<input type='hidden' name='pickTotal' value='".$k."' />";
echo "<input type='hidden' id='point' name='earnedPoint".$k."' value='".$point."' />";
echo "<input type='hidden' id='users' name='userName".$k."' value='".$userPick['user']."' />";

//loop ends

我的更新.php

if(isset($_POST['submit'])){

    $pickTotal=$_POST['pickTotal']; //get the total loop

    for ($p=0;$p<=$pickTotal;$p++){

        $userToBeUpdated=$_POST['userName'.$p]; 
    $userPoint=$_POST['earnedPoint'.$p]; 

       //sort the $userPoint here. 
       //I need to find out who got the most points
       //and list user's place. 1st, 2nd, 3rd...etc. 


       //update my mysql
    }

感谢您的帮助。

【问题讨论】:

    标签: php mysql forms arrays sorting


    【解决方案1】:

    我会提出与马里奥的建议非常相似的建议,但方式略有不同:

    echo "<input type='hidden' id='point' name='user[$k][points]' value='".$point."' />";
    echo "<input type='hidden' id='users' name='user[$k][name]' value='".$userPick['user']."' />";
    

    当您返回 $_POST 时,您将拥有一个像这样的数组:

    $_POST['user'] = array(
        0 => array(
            points => 15,
            name => joe
        ),
        1 => array(
            points => 21,
            name => john
        )
    );
    

    从那里您可以使用usort 来提供自定义排序功能:

    $data = $_POST['user'];
    usort($data, 'usortPost');
    
    function usortPost($a, $b) {
        if ($a['points'] == $b['points']) return 0;
        return $a['points'] < $b['points'] ? 1 : -1;
    }
    

    【讨论】:

    • 不错。 +1。肯定比我的回答更能指出问题。
    • 很好地使用 php php 名​​称语法。不知道还能这么玩。 +1
    【解决方案2】:

    你应该使用 PHP 的特殊形式名称语法,而不是计算 $k 和 $p:

     <input name="earnedPoint[]" value="...">
     <input name="userName[]" value="...">
    

    通过这种方式,您已经将两个参数作为列表接收,$_POST["earnedPoint"][0] 到 $_POST["earnedPoint"][99] 对应于 $_POST["userName"][0]..[99 ].

    然后只映射两个数组:

     $sort_us = array_combine($keys=$_POST["userName"], $values=$_POST["eP"]);
     arsort($sort_us);
    

    这应该让你首先获得最高的。

    【讨论】:

    • 谢谢。 PHP 的特殊形式名称语法非常有用。
    • 我会给你接受的答案,因为你是第一个提出 php 名​​称语法的人。 :D
    【解决方案3】:

    您必须有一个排序条件。

    无论如何,sort 函数应该可以帮助你。

    【讨论】:

      【解决方案4】:

      如前所述,您可以使用 PHP 提供的语法糖:

      echo "<input type='hidden' id='point' name='earnedPoint[{$userPick['user']}]' value='".$point."' />";
      

      你可以像这样在后端处理这个:

      foreach ($_POST['earnedPoint'] as $user => $points) {
          // update your SQL table
      }
      
      asort($_POST['earnedPoint']); // sort array in ascending order, maintain index assoc
      
      // save your data somehow
      

      【讨论】:

        猜你喜欢
        • 2017-12-05
        • 1970-01-01
        • 1970-01-01
        • 2023-01-26
        • 2016-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-25
        相关资源
        最近更新 更多