Sort the $_POST variables

2019-05-30 05:49发布

Might be an easy for you guys. I am trying to sort the $_POST variables that were sent by a form and update the sorted result in mysql. I am not sure how to do it and appreciate it anyone can help me about it.

My 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

My update.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
    }

Thanks for any helps.

4条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-05-30 05:57

You must have a criteria to sort.

Anyway, sort function should help you.

查看更多
该账号已被封号
3楼-- · 2019-05-30 06:08

I would suggest something very similar to what Mario suggested, but in a slightly different way:

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']."' />";

When you get the $_POST back, you'll have an array like this:

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

From there you could use usort to come up with a custom sorting function:

$data = $_POST['user'];
usort($data, 'usortPost');

function usortPost($a, $b) {
    if ($a['points'] == $b['points']) return 0;
    return $a['points'] < $b['points'] ? 1 : -1;
}
查看更多
手持菜刀,她持情操
4楼-- · 2019-05-30 06:17

Instead of counting up $k and $p, you should use PHPs special form name syntax:

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

This way you receive both parameters as list already, $_POST["earnedPoint"][0] till $_POST["earnedPoint"][99] corresponds to $_POST["userName"][0]..[99].

Then just map both arrays:

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

This should get you the highest first.

查看更多
戒情不戒烟
5楼-- · 2019-05-30 06:18

You could, as mentioned previously, use a syntax sugar offered by PHP:

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

You could handle this in the back-end like this:

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
查看更多
登录 后发表回答