Page 1 of 1
How do I use Arrays, posting them and Expoding them?
Posted: Mon Oct 12, 2015 5:27 am
by simonmlewis
I am writing a page where we post people's IDs, hits, points etc to a leaderboard.
So I have a table or row by row. In each row there is a dropdown for their ID (displays their name), and other fields for various things.
I need to post them all in one go (rather than one by one.. 20-30 times!).
So I need to put each row into an array, so there could be 20-30 arrays that all need gathering and "exploding" to enter into a database.
I think each form element is like this:
But I don't know how to make each array a different variable name (array1, array2), and then explode that.
Re: How do I use Arrays, posting them and Expoding them?
Posted: Mon Oct 12, 2015 6:15 am
by requinix
Do not try to get different variables out of it. You have an array. Use the array.
Re: How do I use Arrays, posting them and Expoding them?
Posted: Mon Oct 12, 2015 6:21 am
by simonmlewis
But how? It will be one form, with row after row.
So how do I code up the form element names (userid[])?, and then how to I collate them after posting?
I'm sure the collating would use a while, and array but cannot quite grasp how to collate the entries in POST, and then the action after.
Re: How do I use Arrays, posting them and Expoding them?
Posted: Mon Oct 12, 2015 6:55 am
by requinix
The collating you can do explicitly in your HTML with
Thus "row" is an array with each element being an associative array using keys like "userid". The actual top-level keys (eg, 0) don't actually matter so long as they match up within the row. If you need a dynamic add/remove system in Javascript, I can show you how I currently do it using a general-purpose custom library.
Re: How do I use Arrays, posting them and Expoding them?
Posted: Mon Oct 12, 2015 7:04 am
by simonmlewis
Code: Select all
<td><input type='text' name='points[0][userid]'></td><td><input type='text' name='teamname[0][userid]' value='TESTTeam'>
So these would be...?
Or rather I use 0, 1..2 in the [0] part (which I'd do with a count).
And if that's the case, how do I then collate after submission, to put each row in a database?
Re: How do I use Arrays, posting them and Expoding them?
Posted: Mon Oct 12, 2015 10:10 am
by Christopher
I think it is more like:
Code: Select all
User ID:<input type='text' name='data[0][userid]' value='TESTuser'>
Points:<input type='text' name='data[0][points]' value='TESTpoints'>
Team:<input type='text' name='data[0][teamname]' value='TESTteam'>
Then receive the form and:
Code: Select all
foreach ($data as $row) {
$db->update($row);
}
Re: How do I use Arrays, posting them and Expoding them?
Posted: Tue Oct 13, 2015 5:03 pm
by pickle
I usually do this instead:
Code: Select all
<input type = "text" name = "userid[]"... />
<input type = "text" name = "points[]"... />
<input type = "text" name = "teamname[]"... />
This will give you POSTed information like
Code: Select all
userid => ['user1','user2','user3'],
point => ['8','12','98'],
teamname => ['Eagles','Spartans','Cougars']
You can then iterate through just one of those, and use the current key to access the others:
Code: Select all
foreach($_POST['userid'] as $index=>$userid){
$points = $_POST['points'][$index];
$teamname = $_POST['teamname'][$index];
}