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.
You must have a criteria to sort.
Anyway, sort function should help you.
I would suggest something very similar to what Mario suggested, but in a slightly different way:
When you get the
$_POST
back, you'll have an array like this:From there you could use
usort
to come up with a custom sorting function:Instead of counting up $k and $p, you should use PHPs special form name syntax:
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:
This should get you the highest first.
You could, as mentioned previously, use a syntax sugar offered by PHP:
You could handle this in the back-end like this: