Jump to content

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/190911-arrays-and-radio-buttons/
Share on other sites

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.

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.