Jump to content

Foreach with multiple $_POSTS


OutOfInk

Recommended Posts

I have multiple checkboxes which im submitting data from and need to retrieve that data.  I need to be able to get closewin[$i] and winner[$i] into the same foreach() statement so that it stops defaulting to the first 0 to 8 options; my code is below; I have placed $o++ in there to make all closewin[$i] options different 

 

 

<?php
$fuck = $globalRound[0] - 1; // On round 3
 
if (isset($_POST['subclose'])){
 
foreach ($_POST['winner'] as $index => $value){
$a = $_POST['closewin'];
$b = $value;
$q = mysql_fetch_row(mysql_query("SELECT FROM WHERE `` = '$b[$i]'"));
if ($a[$i] = $q[0]){ $d = $q[0]; $e = $q[1]; }else{ $d = $q[1]; $e = $q[0]; }
mysql_query("UPDATE ");
echo "You have selected $a[$i] as the winner!<br />";
echo "---------------------------<br />";
}
 
echo "<b>You have closed Round $fuck, leaderboard is now updated, ppc credited for winners!</b><br />";
}
?>
 
 
<form action="/p.php" method="post" name="closeg">
<table border='1' cellpadding='2' cellspacing='0' bordercolor='#000000' class='sub2' width="620">
<tr class="innertable"><td align="center" colspan="2"><b>Close Round / Update Leader Board / Credit PPC wins</b></td></tr>
<?php
$i = 0;
$o = 9;
$selectclose = mysql_query("SELECT `id` , `Home` , `Away` FROM `` WHERE `Round` = '$fuck'");
while ($closeselect = mysql_fetch_row($selectclose)){
echo "
<tr class=innertable>
<td>
<input type='hidden' name='winner[$i]' value='$closeselect[0]'>
<input type='checkbox' class='input' name='closewin[$i]' value='$closeselect[1]'>$closeselect[1]
</td><td>
<input type='checkbox' class='input' name='closewin[$o]' value='$closeselect[2]'>$closeselect[2]</td>
</tr>";
$i++;
$o++;
}
?>
<tr><td align="right" colspan="2"><input type="submit" name="subclose" class="input" value="Close Round" /></td></tr>
</table>
</form>
 
 
The html form appears like the following;
Close Round / Update Leader Board / Credit PPC wins
Pleae double check winners before submitting to close the round as once entered, to undo would be impossible.
[checkbox] Hawthorn | [checkbox] Fremantle
[checkbox]Western Bulldogs | [checkbox]Richmond
[checkbox]Adelaide | [checkbox]Sydney
[checkbox]Gold Coast | [checkbox]Brisbane Lions
[checkbox]Collingwood | [checkbox]Geelong
[checkbox]West Coast | [checkbox]St Kilda
[checkbox]GWS Giants | [checkbox]Melbourne
[checkbox]North Melbourne | [checkbox]Port Adelaide
[checkbox]Essendon | [checkbox] Carlton
 
 
and when submitted with all the right hand side 9 to 17 checkboxes selected it defaults to the left options;
 
You have selected Hawthorn as the winner!
---------------------------
You have selected Western Bulldogs as the winner!
---------------------------
You have selected Adelaide as the winner!
---------------------------
You have selected Gold Coast as the winner!
---------------------------
You have selected Collingwood as the winner!
---------------------------
You have selected West Coast as the winner!
---------------------------
You have selected GWS Giants as the winner!
---------------------------
You have selected North Melbourne as the winner!
---------------------------
You have selected Essendon as the winner!

 

Link to comment
Share on other sites

You need to give the checkboxes the same index, not separate ones ($i and $o). I'd setup the index to be the id field returned by your query

 

Also you are best of using radio buttons instead of checkboxes, as only one team can be selected to be the winner for each match not both (unless you are allowing for draws?, in which case you'll want to have a third radio button for that)

 

So I'd setup the while loop as

while ($closeselect = mysql_fetch_row($selectclose)){
echo "
<tr class=innertable>
<td>
<input type='radio' class='input' name='closewin[$closeselect[0]]' value='$closeselect[1]'>$closeselect[1]
</td><td>
<input type='radio' class='input' name='closewin[$closeselect[0]]' value='$closeselect[2]'>$closeselect[2]</td>
</tr>";
} 

Now your foreach loop should display the chosen team correctly.

Edited by Ch0cu3r
Link to comment
Share on other sites

Thanks Ch0cu3r, that helped a lot, the only thing im still having trouble with is carrying the ID which i need to relate to the table row for each of those games. $closeselect[0] is important to be carried as that indexs the specific row that $closeselect[1], $closeselect[2] or Draw relate too. At the moment it now gathers the correct option with the radio checked for any of the options but how can i grab that $closeselect[0]

Link to comment
Share on other sites

You'd loop through the $_POST['closewin'] array. The match id will be the key for that array

//                                key    =>   value
foreach ($_POST['closewin'] as $match_id => $winning_team)
{
    echo '<p>You have chosen the winning team to be <b>' . $winning_team . '</b> for match id <b>#' . $match_id . '</b></p>';
}
Link to comment
Share on other sites

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.