How do I use Arrays, posting them and Expoding them?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I use Arrays, posting them and Expoding them?

Post 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:

Code: Select all

<select name='userid[]'>
But I don't know how to make each array a different variable name (array1, array2), and then explode that.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I use Arrays, posting them and Expoding them?

Post by requinix »

Do not try to get different variables out of it. You have an array. Use the array.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I use Arrays, posting them and Expoding them?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I use Arrays, posting them and Expoding them?

Post by requinix »

The collating you can do explicitly in your HTML with

Code: Select all

<select name="row[0][userid]">
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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I use Arrays, posting them and Expoding them?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I use Arrays, posting them and Expoding them?

Post 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);
}
(#10850)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How do I use Arrays, posting them and Expoding them?

Post 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];
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply