0

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.

2
  • You can create the array key before using it: $_POST['id' . $i]; // Key = id1. However, $_POST['count'] is 6, but id3 is the last id value passed via the $_POST array. Your approach may not be the most suitable (hint: Variables From External Sources). Commented Sep 30, 2011 at 10:30
  • Looks like you are using post data directly. You should sanitize it first before using it. Here is an example of one way of doing it: net.tutsplus.com/tutorials/php/… Commented Oct 1, 2011 at 0:25

7 Answers 7

2

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

So the array is made when the posting is done? Do you know of any more examples of this so i could look into it?
1

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

1 Comment

jensgram - thank you sir, I've been doing lots of Javascript today and always get concat operators mixed up.
0
for($i = 0; i< $_POST['count']; i++){
    //Order input data into an array
    $inputid[$i] = $_POST['id'.$i];

}

Comments

0

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

Comments

0

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

1 Comment

No the amount isn't known, so i can't hard code it like that.
0

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

}

3 Comments

This seems to break the whole script! :S
i have changed the way of generating the post_id. One question about the starting of post variable id is it starts from 0 or 1 ? if it's one you should start your loop from $i = 1
I got it working and it was my own dump fault! :( Well after you help anyway.
0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.