News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Forms and Arrays

Started by mrandall131, December 01, 2007, 06:53:35 PM

Previous topic - Next topic

mrandall131

Ok, I'm coding up a stats program for my gaming team.  I'm trying to create an array of integers in the form and pass that into the file that processes them.  Here's the code for the form....


  for($i=1; $i<11; $i++)
    {
  echo "<tr>
      <td align='center'><font color='#FFFFFF'>Player #".$i."</font></td>
      <td align='center'><select name='player[".$i."]'>";
                            get_teammates($team_group);
      echo "                     </select></td>
          <td align='center'><input type='integer' maxsize='10' name='points[".$i."]' size='5'></td>
          <td align='center'><input type='integer' maxsize='10' name='kills[".$i."]' size='5'></td>
          <td align='center'><input type='integer' maxsize='10' name='assists[".$i."]' size='5'></td>
          <td align='center'><input type='integer' maxsize='10' name='deaths[".$i."]' size='5'></td>
</tr>";
}


When I get into the file that processes the form, the first thing I do is check to see if the player id is greater than 0 (If a player was selected in the drop).  Here's the code I'm using for that....


   for($i=1; $i<11; $i++)
     {
   if ($_POST['player[$i]'] > 0)
     {
   // Process the Data for each player that has stats
   // ......
   // ......
}
}


This doesn't seem to work.  First off, is it possible to do what I am trying to do?  Second, if so, then what am I doing wrong?

suomynona

#1
Failing having the arrays work in that, you could set up the arrays by yourself, with a code like...

Form sends "points1,kills1,assists1,deaths1,points2...deaths10" and all that, then...

$points = array ( 1 => $_POST['points1'], 2 => $_POST['points2']...

Etc, etc. At least I think you could do that, never tried it before.

If you don't need it to be in array form, you could just use the string concat in...


   for($i=1; $i<11; $i++)
     {
      if ($_POST['player[$i]'] > 0)
        {
         // Process the Data for each player that has stats
         // ......
         // ......
       }
    }


And make it:



   for($i=1; $i<11; $i++)
     {
      if ($_POST['player' . $i] > 0)
        {
         // Process the Data for each player that has stats
         // ......
         // ......
       }
    }


And remove the [s and ]s in the form code.

There's probably some other things I've forgotten or don't know, but this should help.

Or maybe your problem is just the $_POST['player[$i]']. Which looks like it is simply querying an array for anything with exactly the name 'player[$i]', which would mean $_POST['player['.$i.']'] might also work...

Small edit: I forgot my array syntax.

mrandall131

Yeah, your first suggestion was how I did it in previous versions, lots of redundant code.  Plus, it's not easily scalable, but I guess it's going to be my best choice for now again.  I have a variable for team size in the competition table to allow this to be nice and scalable....if arrays worked.  LOL!

Tried the $_POST['player['.$i.']'] thing, and the $_POST['player' . $i] thing, but neither worked.  I'm going to recode the form without using arrays and then I'll try to make the array to process the data.

Thanks for trying though.

suomynona

It really should be easy enough to scale, something like...

for($i=1;$i<=$players;$i++)
    {
    $points[$i] = $_REQUEST['points' . $i];
    }


And etc, seeing as arrays don't mind concatenation at all.

mrandall131

Well, I recoded the form to have individual variables and then did what you suggested and created an array in the file that processes the form.  Looks and works good.  Wish there was a way to pass the array in, but this works great.

Thanks!

rsw686

#5
You can access the array with

$_POST['player'][$i]

or loop through it with

foreach ($_POST['player'] as $opt => $val)

I use a POST array on my custom fields mod. Heres the function to save the data. Maybe this will come in use or give you an idea of what you can do to simplify the process by using arrays.


function makeCustomFieldsChanges($memID)
{
global $db_prefix;

if (isset($_POST['custom_fields']) && is_array($_POST['custom_fields']))
{
// Remove all old choices to keep things clean
db_query("
DELETE FROM {$db_prefix}profile_members
WHERE ID_MEMBER = '$memID'", __FILE__, __LINE__);

// Build array of custom fields and values
foreach ($_POST['custom_fields'] as $opt => $val)
if(!empty($val))
$choices[] = "('$opt', '$memID', '$val')";

// Commit the choices to database
if(isset($choices))
db_query("
INSERT IGNORE INTO {$db_prefix}profile_members
(ID_PROFILE, ID_MEMBER, choice)
VALUES " . implode(', ', $choices),  __FILE__, __LINE__);
}
}


One way to simplify the code to save the data would be like this


$sections = array('points','kills','assists','deaths');

foreach($sections as $section)
    foreach($_POST[$section] as $key => value)
The Reptile File
Everything reptile for anyone reptile friendly

Aquaria Talk
Community for freshwater and saltwater aquariums enthusiasts

mrandall131

Thanks, I'll give this a try tonight!

mrandall131

Just wanted to post the final solution for this......

First, setting up the form:

  for ($j=1; $j<$data['max_members']; $j++)
    {  echo "<input type='int' name='player_id[]'>
                  <input type='int' name='score[]'>
                  .... }


Now, to handle the arrays, I did this.....

  $i = 1;
  foreach($player_data as $_POST['player_id'])
    { $local_data['player_id'][$i] = $player_data;
       $i++; }
  $i = 1;
  foreach($player_data as $_POST['score])
    { $local_data['score'][$i] = $player_data;
       $i++; }
.....


Probably a bit excessive, but that's what I did, and it works.  Thanks for all the help guys!

Advertisement: