Array with dynamic Radio buttons

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
snapy
Forum Newbie
Posts: 4
Joined: Fri Sep 09, 2005 2:03 am

Array with dynamic Radio buttons

Post by snapy »

Hello all,

I think the title correctly represents what it is that I wish to do.

I am creating a footy tips site, which allows users to place tips on who they think will win in each of the 8 weekly games.

On the tipping page, the games are inserted via an array, and the int's are changed to represent names:

Code: Select all

//Connect to the db
									$conn = pg_pconnect("dbname=footytips user= password=");
									if (!$conn) {
    									echo "An error occured.\n";
    											exit;
									}
				//setup & run the query to the database
						$query1 = pg_query($conn, "SELECT * FROM game WHERE round = $data");
						
				//setup ref to query1
	    while($row1 = pg_fetch_array($query1)) {
            $game = $row1['game'];
            $date = $row1['date'];
            $time = $row1['time'];
            $ground_id = $row1['ground_id'];
            $team1_id = $row1['team1_id'];
            $team2_id = $row1['team2_id'];
     
     $result1 = pg_query($conn, "SELECT name FROM team WHERE $team1_id = id");
	  while($row2 = pg_fetch_array($result1)) {
	  	$team1_id = $row2['name'];       
      
      $result1 = pg_query($conn, "SELECT name FROM team WHERE $team2_id = id");
	  while($row3 = pg_fetch_array($result1)) {
	  	$team2_id = $row3['name']; 
	  	
	  	$result1 = pg_query($conn, "SELECT name FROM ground WHERE $ground_id = id");
	  while($row4 = pg_fetch_array($result1)) {
	  	$ground_id = $row4['name'];
It then gets put dynamically into a table row (each record) and the while arrays are closed:

Code: Select all

print "\t<tr>\n";
												print "<td>".$row1['game']."</td> \n";
												print "<td>".$row1['date']."</td> \n";
												print "<td>".$row1['time']."</td> \n";
												print "<td>".$row4['name']."</td> \n";
											print	 "<td>".$row2['name']."</td> \n";
											echo ('<td><input type="radio" name="winner[id]"'.'value="home"/></td>');
												print "<td>".$row3['name']."</td> \n";
												echo ('<td><input type="radio" name="winner[id]"'.'value="away"/></td>');
												
											print "\t</tr>\n";
											
											
											
											}
}
}
}

										?>

As you can see I am using radio buttons to allow users to choose there winner, but atm the radio buttons are all grouped together and so for the different games, only 1 radio button can be selected.

What I want is for each game, the user select 1 radio button, which represents the team they are pciking for a winner.

Can anyone out there, point me in the right direction, or lead me to a tutorial.

thank you very much,

Matt
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

$result1 = pg_query($conn, "SELECT name FROM team WHERE $team1_id = id"); 
      while($row2 = pg_fetch_array($result1)) { 
          $team1_id = $row2['name'];        
       
      $result1 = pg_query($conn, "SELECT name FROM team WHERE $team2_id = id"); 
      while($row3 = pg_fetch_array($result1)) { 
          $team2_id = $row3['name']; 
           
          $result1 = pg_query($conn, "SELECT name FROM ground WHERE $ground_id = id");
I dont know how this part of code gets executed correctly.
you see the id is without a $ before it.
it would execute without errors but you wont have any value for the id.

edit:
you are not closing the while loops for $rows2 and $rows3 as well.

can you draw a picture(a static page) of how you wanted the output to be??? I can not really understand what your various fields are doing??
snapy
Forum Newbie
Posts: 4
Joined: Fri Sep 09, 2005 2:03 am

Post by snapy »

raghavan20,

The code does work, if this site allowed me to post a screen shot, it would probably be much more easier to understand.

The whiles that you pointed out, change the variable $team1_id = $row1['team1_id']; in the first section of code I gave you, from an id number to a team name.

And, for the not closing the while loops, I can also assure you that they are all closed.

I have discovered the solution to my problem, I had to increment the name:

Code: Select all

$i = 1;

...code...

echo ('<td><input type="radio" name="winner[id]'.$i.'" value="home'.$i.'"/></td>');

...more code...

$i ++;
This way in the actual page source it comes up:

Code: Select all

....code...
<td><input type="radio" name="winner[id]1" value="home1"/></td><td>Essendon</td> 
<td><input type="radio" name="winner[id]1" value="away1"/></td>	</tr>
	<tr>
....more code.....
 
<td><input type="radio" name="winner[id]2" value="home2"/></td><td>Kangaroos</td> 
<td><input type="radio" name="winner[id]2" value="away2"/></td>	</tr>
	
.....and more code....
<td><input type="radio" name="winner[id]3" value="home3"/></td><td>Collingwood</td> 
<td><input type="radio" name="winner[id]3" value="away3"/></td>	</tr>
.... there we go thats it for now.....

thank you for taking the time to look at my problem, i do very much appreciate it.

Thank you,

Matt
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not something like

Code: Select all

echo ('<td><input type="radio" name="winner['.$i.']" value="home"/></td>');
echo ('<td><input type="radio" name="winner['.$i.']" value="away"/></td>');
:?:
Post Reply