variables - PHP $_POST values

887

i have a little problem with storing the $_POST data, think i might be confusing myself a little.

So i have some data being posted from a loop, this data is posted as id and points, but each has a number to it so if three records are being posted we'll have

id1, id2, id3 points1, points2, points3

to collect. Now i'm using a for loop to go through the data and post it into two arrays to work with. The problem is When I want to store the data i have to use one of the names listed above ie id1 or id2. Here is my code

for($i = 0; i< $_POST['count']; i++){
    //Order input data into an array
    $inputid[$i] = $_POST['id1'];

}

Now the number part of the 'id' of the $_POST['id1'] has to be the same as $i in the for loop so it will increment as the loop does.

Thanks for the help and i hope i explained the question right.

661

Answer

Solution:

Why not name the inputs:name="id[1]" andname="points[1]" so you'll have$_POST['id'][...] and$_POST['points'][...] arrays to work with?

Reference: Variables From External Sources (specifically Example #3).

443

Answer

Solution:

Firstly, don't use POST variables in loops or anything else unless you've checked them out first to make sure they don't contain anything nasty.

You could try this within the loop:

$idnumber = "id" . $i;
$inputid[$i] = $_POST[$idnumber];
670

Answer

Solution:

for($i = 0; i< $_POST['count']; i++){
    //Order input data into an array
    $inputid[$i] = $_POST['id'.$i];

}
761

Answer

Solution:

Simply concatenate the string in the index:

for($i = 0; i< $_POST['count']; i++){
    //Order input data into an array
    $inputid[$i] = $_POST['id' . ($i+1)];
}
858

Answer

Solution:

If I understand this question, there are already a known amount of inputs that are going to be posted, so I don't understand why you need a loop at all for adding them to the array. Why not do this:

$value = array($_POST['id1'], $_POST['id2'], $_POST['id3'], $_POST['points1'], $_POST['points2'], $_POST['points3']);

than loop like this:

for(x=0;x<$value.count;x++){
    $value[x]=$value.$x;
}

That should work

18

Answer

Solution:

if i am not going wrong you want the posted id same as the increment var $i try this out

for($i = 0; $i< $_POST['count']; $i++){
    $post_id = 'id'.$i;
    $inputid[$i] = $_POST[$post_id];

}
344

Answer

Solution:

I think you can go for something like this:

for($i = 0; $i< $_POST['count']; $i++){
//Order input data into an array
$inputid[$i] = $_POST["id$i"];
}

Is this what you want?

People are also looking for solutions to the problem: xml - Is there an xml_encode() like json_encode() in PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.