Dyanamic Checkboxes and Associative Arrays

  • JakeBurgh
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Sep 05, 2010
  • Posts: 5
  • Status: Offline

Post September 5th, 2010, 7:35 am

Hey,

I'm making a basic football simulator for a play by mail game where users submit their teamsheets through the post. I'm automating the system so that I select the teams on a website which then simulates the match for me and returns a result.

The home/away teams are presented in a table, taken from a MySQL database which checkboxes next to each player to select them to play in that match here:

PHP Code: [ Select ]
$p=mysql_query("SELECT * FROM fixtures WHERE fixtureid='{$_GET['match']}' LIMIT 1", $c);
$pa=mysql_fetch_array($p);
print "<b><center><font size=+1>{$pa['homename']} v {$pa['awayname']}</font><br />
Division {$pa['tournament']} fixture | Turn {$pa['turn']}<br /><br />";
if ($pa['played'] > 0)
{
print "<b>This match has already been simulated. If you have entered the incorrect team or are unhappy with the result then please continue to select the squad.<br /><br /></b>";
}
$ht=mysql_query("SELECT * FROM players WHERE teamcode={$pa['hometeam']} AND rating2 > 0 ORDER BY posorder ASC", $c);
$at=mysql_query("SELECT * FROM players WHERE teamcode={$pa['awayteam']} AND rating2 > 0 ORDER BY posorder ASC", $c);
 
print "<form action='playmatch.php?stage=B&match=". $_GET['match']. "' method=post>
<div style='position: absolute; left: 200px; top: 80px;  width: 300px; padding: 1em;'>";
 
print "<font size=-1>
<table border=0>";
while ($h=mysql_fetch_array($ht))
{
 
print "<tr><td width=2%><input type='checkbox' value='" . $h['playerid'] ."' name='home[]'></td><td>
{$h['surname']}</td>
<td>{$h['pos1']}";
 
  1. $p=mysql_query("SELECT * FROM fixtures WHERE fixtureid='{$_GET['match']}' LIMIT 1", $c);
  2. $pa=mysql_fetch_array($p);
  3. print "<b><center><font size=+1>{$pa['homename']} v {$pa['awayname']}</font><br />
  4. Division {$pa['tournament']} fixture | Turn {$pa['turn']}<br /><br />";
  5. if ($pa['played'] > 0)
  6. {
  7. print "<b>This match has already been simulated. If you have entered the incorrect team or are unhappy with the result then please continue to select the squad.<br /><br /></b>";
  8. }
  9. $ht=mysql_query("SELECT * FROM players WHERE teamcode={$pa['hometeam']} AND rating2 > 0 ORDER BY posorder ASC", $c);
  10. $at=mysql_query("SELECT * FROM players WHERE teamcode={$pa['awayteam']} AND rating2 > 0 ORDER BY posorder ASC", $c);
  11.  
  12. print "<form action='playmatch.php?stage=B&match=". $_GET['match']. "' method=post>
  13. <div style='position: absolute; left: 200px; top: 80px;  width: 300px; padding: 1em;'>";
  14.  
  15. print "<font size=-1>
  16. <table border=0>";
  17. while ($h=mysql_fetch_array($ht))
  18. {
  19.  
  20. print "<tr><td width=2%><input type='checkbox' value='" . $h['playerid'] ."' name='home[]'></td><td>
  21. {$h['surname']}</td>
  22. <td>{$h['pos1']}";
  23.  


The form then continues and it is the same for the away team except the name for the checkbox is 'away[]'.

Where I am having trouble is I do not know how to interpret the returned data for the form on the next page.

I need to select every player (who has been selected) from the database using the player's unique id number from the value of:

PHP Code: [ Select ]
value='" . $h['playerid'] ."'


I have tried various foreach/while statements but they do not seem to return anything. At the moment I have
PHP Code: [ Select ]
print "{$_POST['home']}";
 
  1. print "{$_POST['home']}";
  2.  

On the return screen which prints the word 'Array'. Therefore I know the information is being sent through the form as an associative array, but I do not know how to access and use the values returned.

Any help is greatly appreciated, Jake

PS, I know my php and sql is slightly outdated/strange, basically because I learnt it all from MCCode a few years back, but still, it does the job.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post September 5th, 2010, 7:35 am

  • JakeBurgh
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Sep 05, 2010
  • Posts: 5
  • Status: Offline

Post September 6th, 2010, 5:47 am

Problem solved.

PHP Code: [ Select ]
$home=$_POST['home'];
print "$home[0]";
print "$home[1]";
 
  1. $home=$_POST['home'];
  2. print "$home[0]";
  3. print "$home[1]";
  4.  


$home[0] then returns the playerid of the first player selected, $home[1] the second etc. etc.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post September 6th, 2010, 11:34 am

Thanks for posting the solution, makes sense since it sounds like it $_POST['home'] is just an array of values. Glad you were able to figure it out on your own. So just for reference you should be able to access the variable like this as well:

PHP Code: [ Select ]
print $_POST['home'][0];
print $_POST['home'][1];
  1. print $_POST['home'][0];
  2. print $_POST['home'][1];
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Joined: Mar 12, 2007
  • Posts: 6228
  • Loc: South-Africa
  • Status: Offline

Post September 7th, 2010, 2:48 am

if you don't know how many $_POST['home'] items there will be in that array, you can use a foreach loop to loop through them, as follows:

PHP Code: [ Select ]
<?php
  foreach($_POST['home'] as $key=>$value){
    //do something here
  }
?>
  1. <?php
  2.   foreach($_POST['home'] as $key=>$value){
  3.     //do something here
  4.   }
  5. ?>
Let's leave all our *plum* where it is and go live in the jungle ...

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 162 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.