Jump to content

Problem with arrays...


jcbarr

Recommended Posts

Okay so here is the deal. I have a form that sends a bucnh of variables to a php script. It sends a them like so pos[] league[] stat[] name[]

It sends them to this script

[code]//Get Variables
$create = $_GET['CREATE'];
$edit = $_GET['EDIT'];
$add = $_GET['ADD'];
$view = $_GET['VIEW'];
?>

<?PHP if ($add=="Y") {

foreach($_POST as $k=>$v)
{
  if($k=='pos')
  {
      foreach($_POST[$k] as $v)
      {
          if($v!==""){
          $sql="INSERT INTO ballot (POS) VALUES ('$v')";
          mysql_query($sql); }
      }
  }
  if($k=='league')
  {
      foreach($_POST[$k] as $v)
      {
          $sql="UPDATE ballot SET LEAGUE = '$v' WHERE LEAGUE = '' LIMIT 1";
          mysql_query($sql);
      }
  }
  if($k=='player')
  {
      foreach($_POST[$k] as $v)
      {
          $sql="UPDATE ballot SET NAME = '$v' WHERE NAME = '' LIMIT 1";
          mysql_query($sql);
      }
  }
  if($k=='stat')
  {
      foreach($_POST[$k] as $v)
      {
          $sql="UPDATE ballot SET STAT = '$v' WHERE STAT = '' LIMIT 1";
          mysql_query($sql);
      }
  }
}

                      }[/code]

Now the problem is that it doesn't seem to be doing them in the right order. For whatever reason the arrays aren't going over ordered the same or something. The form code looks like this.

[code]<table align="center" cellpadding="4" border="1">
<tbody id="T1" >
  <tr>
      <td colspan="5" bgcolor="#3399FF" align="center"><b>UNION LEAGUE</b></td>
  </tr>
  <tr>
      <td bgcolor="#FFFFCC" align="center"><b>POS</b></td>
      <td bgcolor="#FFFFCC" align="center"><b>PLAYER</b></td>
      <td bgcolor="#FFFFCC" align="center"><b>STAT LINE</b></td>
  </tr>
      <form method="post" action="allstar.php?ADD=Y">
  <tr>
      <td bgcolor="#FFFFCC" align="center"><select name="pos[]">
                                          <option><option>P
                                          <option>C
                                          <option>1B
                                          <option>2B
                                          <option>3B
                                          <option>SS
                                          <option>LF
                                          <option>CF
                                          <option>RF</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="player[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="stat[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <select name="league[]"><option><option>UL</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <span onclick="AddRow(); return false;" >Add</span>
      </td>
  </tr>
  <tr style="display:none;" >
      <td bgcolor="#FFFFCC" align="center"><select name="pos[]">
                                          <option><option>P
                                          <option>C
                                          <option>1B
                                          <option>2B
                                          <option>3B
                                          <option>SS
                                          <option>LF
                                          <option>CF
                                          <option>RF</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="player[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="stat[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <select name="league[]"><option><option>UL</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <span onclick="AddRow(); return false;" >Add</span>
      </td>
  </tr>
  <tr style="display:none;" >
      <td bgcolor="#FFFFCC" align="center"><select name="pos[]">
                                          <option><option>P
                                          <option>C
                                          <option>1B
                                          <option>2B
                                          <option>3B
                                          <option>SS
                                          <option>LF
                                          <option>CF
                                          <option>RF</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="player[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="stat[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <select name="league[]"><option><option>UL</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <span onclick="AddRow(); return false;" >Add</span>
      </td>
  </tr>
  <tr style="display:none;" >
      <td bgcolor="#FFFFCC" align="center"><select name="pos[]">
                                          <option><option>P
                                          <option>C
                                          <option>1B
                                          <option>2B
                                          <option>3B
                                          <option>SS
                                          <option>LF
                                          <option>CF
                                          <option>RF</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="player[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="stat[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <select name="league[]"><option><option>UL</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <span onclick="AddRow(); return false;" >Add</span>
      </td>
  </tr>
  <tr style="display:none;" >
      <td bgcolor="#FFFFCC" align="center"><select name="pos[]">
                                          <option><option>P
                                          <option>C
                                          <option>1B
                                          <option>2B
                                          <option>3B
                                          <option>SS
                                          <option>LF
                                          <option>CF
                                          <option>RF</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="player[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="stat[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <select name="league[]"><option><option>UL</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <span onclick="AddRow(); return false;" >Add</span>
      </td>
  </tr>
  <tr style="display:none;" >
      <td bgcolor="#FFFFCC" align="center"><select name="pos[]">
                                          <option><option>P
                                          <option>C
                                          <option>1B
                                          <option>2B
                                          <option>3B
                                          <option>SS
                                          <option>LF
                                          <option>CF
                                          <option>RF</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="player[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="stat[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <select name="league[]"><option><option>UL</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <span onclick="AddRow(); return false;" >Add</span>
      </td>
  </tr>
  <tr style="display:none;" >
      <td bgcolor="#FFFFCC" align="center"><select name="pos[]">
                                          <option><option>P
                                          <option>C
                                          <option>1B
                                          <option>2B
                                          <option>3B
                                          <option>SS
                                          <option>LF
                                          <option>CF
                                          <option>RF</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="player[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <input type="text" name="stat[]">
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <select name="league[]"><option><option>UL</select>
      </td>
      <td bgcolor="#FFFFCC" align="center">
            <span onclick="AddRow(); return false;" >Add</span>
      </td>
  </tr>[/code]

There is a bunch more of it, but I don't think I need to go on.

So, anyone have a suggestion to make the arrays for each form variable line up correctly so that this will work?
Link to comment
Share on other sites

here's a way to keep the related variable together
[code]
<?php
if (isset($_POST['submit'])) {
foreach ($_POST['pos'] as $k => $pos) {
$league = $_POST['league'][$k];
$stat = $_POST['stat'][$k];
$name = $_POST['name'][$k];
if ($pos != '') {
mysql_query ("INSERT INTO mytable (pos, league, stat, name)
          VALUES ('$pos', '$league', '$stat', '$name')");
}
}

}
?>
<FORM method='POST'>
<table border='0'>
<TR>
<TD>Pos</TD>
<TD>League</TD>
<TD>Stat</TD>
<TD>Name</TD>
</TR>
<?php
for ($i=0; $i<5; $i++) {
    echo '<TR>
<TD><input type="text" name="pos[]"></TD>
<TD><input type="text" name="league[]"></TD>
<TD><input type="text" name="stat[]"></TD>
<TD><input type="text" name="name[]"></TD>
</TR>
';
}
?>
</table>
<input type="submit" name="submit" value="Add">
</FORM>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.