Arrays and Forms Help

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
kbirka
Forum Newbie
Posts: 4
Joined: Wed Aug 29, 2007 1:56 pm

Arrays and Forms Help

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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')
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
kbirka
Forum Newbie
Posts: 4
Joined: Wed Aug 29, 2007 1:56 pm

Insert Statement

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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)
kbirka
Forum Newbie
Posts: 4
Joined: Wed Aug 29, 2007 1:56 pm

Getting Closer!

Post 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
kbirka
Forum Newbie
Posts: 4
Joined: Wed Aug 29, 2007 1:56 pm

Figured it out I I think

Post 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?
Post Reply