Page 1 of 1

Arrays and Forms Help

Posted: Wed Aug 29, 2007 7:08 pm
by kbirka
I need help figuring out how to name input tags if I want to create and array with the information to so I can input the values into a table in a databse. I have a list of players that have a last name, first name, jersey number, and postion. I gave all the input tags that same name to start with.

Code: Select all

<?php
		  echo "$school $team_opt<br>";
		  echo "$division<br>";
		  $i=0;
		  foreach ($free_agent_team as $free_agent_team) {
		  
		  $sql="SELECT playeridnum FROM free_agents WHERE free_agent_ID='$free_agent_team'";
		  $mysql_result=mysql_query($sql);
		  while ($row=mysql_fetch_array($mysql_result)) {
			$playeridnum=$row["playeridnum"];
			} 
		  
		  $sql="SELECT lastname,firstname FROM registration WHERE playeridnum='$playeridnum'";
		  $mysql_result=mysql_query($sql);
		  while ($row=mysql_fetch_array($mysql_result)) {
		  $lastname=$row["lastname"];
		  $firstname=$row["firstname"];
			} 
		  
		  echo "<tr class=\"style53\"><td>$free_agent_team<input type=hidden name=newteam[] value=$free_agent_team>
		  </td><td>$playeridnum<input type=hidden name=newteam[] value=$playeridnum></td><td>
		  <input type=hidden name=newteam[] value=$lastname>$lastname, $firstname
		  <input type=hidden name=newteam[] value=$firstname></td><td>
		  <div align=\"center\"><input name=\"newteam[]\" type=\"text\" size=\"2\" maxlength=\"2\" class=\"style53\">
		  </div></td><td>
		  <div align=\"center\"><select name=newteam[] class=\"style53\">
            <option value=\"O\">Offense</option>
            <option value=\"D\">Defense</option>
            <option value=\"G\">Goalie</option>
          </select></div></td><td><div align=\"center\"><input type=\"checkbox\" name=newteam[] value=\"1\"></div></td>
		  <td><div align=\"center\"><input type=\"checkbox\" name=newteam[] value=\"1\"></div></td>		  
		  </tr>";
		  		  $i++;
				  }
		  ?>
This gives me the list of players I need. I need to know where to go from here to get my sql statements.

Posted: Wed Aug 29, 2007 8:33 pm
by s.dot
Your array will be in $_POST['newteam'].

To get a comma delimited list

Code: Select all

$newteams = implode("','", $_POST['newteam']);
Which can then be used in a query.

Code: Select all

select * from `table` where `team` IN ('$newteams')

Insert Statement

Posted: Wed Aug 29, 2007 11:57 pm
by kbirka
What I really need to do is create and INSERT statement for each set of records so I can insert them in to a table in database. I added your code in and got the comma delimited values, but I really need them in separate groups based on their id.

Posted: Thu Aug 30, 2007 12:38 am
by Kieran Huggins
You may want to name your inputs in this manner:

Code: Select all

<input name="player['<?=$id?>']['name']" value="Team Name"/>
<input name="player['<?=$id?>']['position']" value="Team Name"/>
/* ... */
That way you can access the variables like so:

Code: Select all

foreach($player as $p){
   MySQL_query("INSERT INTO `players` (".implode(', ',array_keys($p)).") VALUES ('".implode('\',\'',$p)."')");
}
(untested)

Getting Closer!

Posted: Thu Aug 30, 2007 10:13 pm
by kbirka
Thanks Kieran, this seems to work well here is the end result...

Code: Select all

INSERT INTO `players` (\'free_agent_team\', \'playeridnum\', \'lastname\', \'firstname\', \'number\', \'position\') VALUES ('29','1188007541','FirstName','Lastname','12','O')
I have backslashes in the first part of the statement which mySql does not like. Also I know this seems like it should an easy thing to figure out, but I haven't figured out how to break out keys and value into their own variables within the for loop. For example..

Code: Select all

$freeagentid="";
$playeridnum="";
Thanks in advanced

Figured it out I I think

Posted: Sat Sep 01, 2007 2:38 pm
by kbirka
I took out the single quotes in my original form. That seem to make it right. Still looking to break out each value with it's key into a variable. Would I use explode function to do this?