damo87 Posted February 4, 2010 Share Posted February 4, 2010 Note: I posted a topic earlier today on this (array and radio buttons for football tipping), but because I have moved on to a different method, with almost completely different code I thought I'd start a new topic. I am creating a football tipping application. For a specified round of the football season, I need a form to pair up each set of two teams who are playing, show details of the match and allow the user to select one of the teams with a radio button. There are three tables involved, and I have included the create table scripts at the end of this post. I have that part of things working. What I am having trouble with is processing the data from the form. The data that is sent to be processed consists of 8 different arrays. This makes sense to me because there are 8 games each week, therefore 8 sets of radio buttons. In the first output below, the '[17]' is the id of the game, and the "14" is the teamid that has been selected: [17]=> string(2) "14" [18]=> string(2) "16" [19]=> string(2) "12" [20]=> string(2) "13" [21]=> string(1) "4" [22]=> string(1) "2" [23]=> string(2) "17" [24]=> string(1) "8" I just need to insert the teamid into the selections table (along with the userid and roundid, which I don't need from the array). I have hardcoded the ['17'] in the code below to check if the insert statement actually inserts into the database as expected, and it does. But obviously I need it to dynamically loop through all eight arrays, and I am really unsure how to go about that. Or is there a better way to set up the radio buttons, so that the data is sent as one array?? If anyone could suggest how to fix this, either at the form or at the processing stage I would be very grateful. Thanks. if($_POST['17']) { foreach((array)$_POST['17'] as $team) { $query = "Insert into selections (bookfaceuserid, round, team) Values ($user, '3', $team)"; $result = mysql_query($query) or die("Query failed : " . mysql_error()); } } $query="SELECT games.id, hometeam, awayteam, teams.name AS 'hometeamname', teams2.name AS 'awayteamname' FROM games, teams, teams AS teams2 WHERE games.round =3 AND (games.hometeam = teams.id AND games.awayteam = teams2.id)"; ?> <form action="round3.php" method="post"> <input type="hidden" name="add" value="true" /> <input type="hidden" name="userid" value="<?php echo $user ?>" /> <? // printing the radio buttons select command $result = mysql_query ($query); while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<input type = 'radio' name=$nt[id] value=$nt[hometeam]>$nt[hometeamname]<input type = 'radio' name=$nt[id] value=$nt[awayteam]>$nt[awayteamname]<p>"; /* Option values are added by looping through the array */ } ?> <br><br> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/190911-arrays-and-radio-buttons/ Share on other sites More sharing options...
taquitosensei Posted February 4, 2010 Share Posted February 4, 2010 you could do something like this <input type='radio' name='radioarray[$roundid]' value='$teamid'> then foreach($POST['radioarray'] as $roundid=>$teamid) { // do you sql here } Quote Link to comment https://forums.phpfreaks.com/topic/190911-arrays-and-radio-buttons/#findComment-1006784 Share on other sites More sharing options...
damo87 Posted February 4, 2010 Author Share Posted February 4, 2010 Thanks for putting me on the right track. I've tweaked things a bit, so here are the bits that are different from the original code: $gameid = $nt[id]; echo "<input type = 'radio' name='radioarray[$gameid]' value=$nt[hometeam]>$nt[hometeamname]<input type = 'radio' name='radioarray[$gameid]' value=$nt[awayteam]>$nt[awayteamname]<p>"; and then in the processing part: if($_POST['radioarray']) { foreach((array)$_POST['radioarray'] as $team) { Thanks again for you help. Quote Link to comment https://forums.phpfreaks.com/topic/190911-arrays-and-radio-buttons/#findComment-1007055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.