views:

68

answers:

4

Hello guys 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.

+1  A: 

You must have a criteria to sort.

Anyway, sort function should help you.

vladv
Thanks for the tip. +1
Jerry
+3  A: 

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.

mario
Thank you. The PHPs special form name syntax is very helpful.
Jerry
I would give you accepted answer since you are the first person bring up the php name syntax. :D
Jerry
+2  A: 

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;
}
nickf
nice. +1. Definitely more pointed to the problem then my answer.
vladv
Nice use of php php name syntax. Didn't know it could play like this. +1
Jerry
+1  A: 

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
msakr
nice one! thanks for the use of php syntax a lot. +1.
Jerry