Sort the $_POST variables

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.

-------------Problems Reply------------

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.

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;
}

You must have a criteria to sort.

Anyway, sort function should help you.

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

Category:php Views:0 Time:2010-05-31

Related post

  • PHP $_POST variables are sometimes empty 2009-12-04

    I am a PHP novice and I am having a problem with POST variable sometimes being empty when I submit them. The part which is making this difficult for me to figure out is that this doesn't happen every time, and I can usually get the post data in my PH

  • MVC pattern and $_POST variables 2009-12-20

    I am building a php/mysql based framework and cms , and I got stucked into passing variables using post method , from a form located in one controller, to another controller. More exactly , i built a form for changing languages. this form is located

  • PHPUnit - test MVC controller with $_POST variable 2009-12-20

    I'm starting work with PHPUnit with Kohana. My application have many controllers which simply takes data from some form, validates and inserts/updates/deletes into/-/from database. How should I write a test cases for that. I know that if I want to te

  • Why are $_POST variables getting escaped in PHP? 2010-03-22

    When my PHP script receives data from an AJAX POST request, the $_POST variables are escaped. The really strange thing is that this only happens on my production server (running PHP 5.2.12 on Linux) and not on my local server (running PHP 5.3.1 on Wi

  • Losing reference to $_post variable? 2010-03-26

    In the code below, the echo at the top returns true, but the echo at the bottom returns nothing. Apparently the code in between is causing me to lose a reference to the $_post variable? <?php echo "in category: ".in_category('is-sidebar'); //RETUR

  • null $_POST variables if 3rd party cookies blocked on FF & Chrome 2010-08-10

    I'm trying to build a site that does not rely on the user enabling 3rd party cookies. For some reason, I'm noticing null $_POST variables from a simple only in Firefox and Chrome when 3rd party cookies are blocked (IE is fine). This is only with POST

  • Get unlimited $_POST variables & list them 2010-08-20

    I want to receive unlimited $_POST variables including an additional number and turn them into arrays like paypal form does. Example: <form action="mysite"> <input name="productname" /> <input name="productname1" /> <input name="

  • How do you output several lines of the $_POST variable 2011-01-14

    How do I output several lines of the $_POST variable ? When I keep outputting the result I only get the last $_POST variable Thanks for helping --------------Solutions------------- If you want more detailed information about what's being stored in $_

  • PHP need to trim all the $_POST variables 2011-01-17

    Need you help in an unusal situation. I need to trim all the $_POST variables. Is there any way I can do it at a single shot i.e., using a single function? I know trim($_POST) won't do, I have to make some function like function sanatize_post(){ fore

  • $_POST variables coming through empty in IE7 for one subfolder 2011-03-22

    I've having trouble with $_POST variables not being received from forms within a PHP website. To clarify the problem, I created this simple PHP script "POST_test.php": <? print "POST:<pre>"; print_r($_POST); print "</pre>"; ?> <f

  • What do, if $_POST variables are missing? 2011-03-29

    What should an application do, if a $_POST variable is missing, which is required to perform the action? For example, imagine I have a form with an <input name="title"> and I have some page, which processes the resulting POST request. Now, what

  • PHP $_POST variable does not process input fields generated by Javascript 2011-05-20

    I have two files, (file1 and file2). file1 includes file2 in a PHP include statement. File1 also contains a form and prints out all $_POST variables. File2 uses a Javascript button to dynamically change the value in an input field. The problem is tha

  • Can you have a PHP $_POST variable that is set to an array? 2011-05-28

    I am using AJAX to put some data in my database. I am using JSON to submit the data to a PHP page. I am using a POST request. Can you set one of the POST variables as an array? Below I have the full AJAX request, but here is the part where I am setti

  • $_POST variable empty in php 2011-07-04

    I saw a few posts here that have the same problem but none of them are a simple as mine and hence the post. I am new to php and I see that the $_POST and $_REQUEST variable are both empty. I can get the headers using the apache_request_headers() and

  • PHP listener script that can read incoming $_REQUEST or $_POST variables 2011-07-22

    I am trying to write a PHP script that listens for incoming $_REQUEST or $_POST variables that are sent by a web application as part of a 2-way communication. HTTP GET calls will be made to the web app in format similar to this. The WEBAPP will then

  • Problems using enctype="multipart/form-data" with $_POST variables 2011-10-14

    When setting the enctype to multipart/form-data in an HTML form, $_POST variables will not be set. The variables are set without the enctype but it must be in order to use the $_FILES array. Is there an alternative to using the $_FILES array? Bug rep

  • Array_Map on $_POST variables from a multiple select list 2011-11-23

    I am trying to run an array_map (two, technically) on all values received from a $_POST variable, from a multiple select list. Here's the code: $culture = array_map('mysql_real_escape_string', array_map('html2txt', $_POST['culture[]'])); Should this

  • How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP? 2011-12-01

    When I press the 'refresh' button on my browser, it seems that $_POST variable is preserved across the refresh. If I want to delete the contents of $_POST what should I do? Using unset for the fields of $_POST did not help. Help? Thanks! ------------

  • loop through $_POST variables with similar names 2011-12-19

    I have several $_POST variables, they are $_POST['item_number1'] $_POST['item_number2'] and so on I need to write a loop tha displays the values of all the variables (I don't know how many there are). What would be a simplest way to go about it? Also

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.101 (s). 11 q(s)