Failing_Solutions Posted June 15, 2011 Share Posted June 15, 2011 I have a set of 80 checkboxes, 1 to 10 of them will be posted. This creates an array. I was able to see the values by using this: if (isset($_POST['checkbox'])) { echo "Playing Numbers "; foreach($_POST['checkbox'] as $checkbox){ echo $checkbox . " "; } Now I'd like to use those values to run SQL select statements. But I don't know how to reference each checkbox value. I've tried $checkbox[0] but that doesn't seem to work Is there a way to assign something like $num1 = $checkbox[0] $num2 = $checkbox[1] $num3 = $checkbox[2] ... etc.. Obviously that doesn't work but that is what I was thinking. Or is there a way to reference each checkbox in a SQL select statement like... $quer=mysql_query("SELECT * FROM june122011 WHERE $checkbox[0] IN (`A`,`B`,`C`,`D`,`E`,`F`,`G`,`H`,`I`,`J`,`K`,`L`,`M`,`N`,`O`,`P`,`Q`,`R`,`S`,`T`) AND $checkbox[1] IN (`A`,`B`,`C`,`D`,`E`,`F`,`G`,`H`,`I`,`J`,`K`,`L`,`M`,`N`,`O`,`P`,`Q`,`R`,`S`,`T`) AND Game_Date ='$select'"); Here you can see I'm getting the values.. just don't know how to put them into a variable so I can use it to run the query. any thoughts, ideas is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/239454-how-can-you-set-a-unique-variable-for-each-value-in-an-array/ Share on other sites More sharing options...
redixx Posted June 15, 2011 Share Posted June 15, 2011 Try this: $where = ''; $range = implode(',',range('A','T')); foreach($_POST['checkbox'] as $checkbox) { $where .= "'" . $checkbox . "' IN (" . $range . ") AND "; } $where .= "Game_Date='" . $select . "'"; $query = mysql_query("SELECT * FROM june122011 " . $where); Quote Link to comment https://forums.phpfreaks.com/topic/239454-how-can-you-set-a-unique-variable-for-each-value-in-an-array/#findComment-1230127 Share on other sites More sharing options...
Failing_Solutions Posted June 15, 2011 Author Share Posted June 15, 2011 Thanks for the reply, I think I understand most of that, It looked like though it would only process 1 number at a time. Unless I looked at it wrong. While I need it to check the all number selected in the checkboxs so it would have to have up to 9 AND statements in the select. Where checkbox[0] AND checkbox[1] etc.... That why I was hoping to be able to extract each checkbox value and turn them into seperate variables so I could write a case statement based on how many checkboxes were selected and then the 10 queries. That said I did try to run it with just 1 number and got an error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\PhpStuff\keno.php on line 90 I used this code to test the query: $where = ''; $range = implode(',',range('A','T')); foreach($_POST['checkbox'] as $checkbox) { $where .= "'" . $checkbox . "' IN (" . $range . ") AND "; } $where .= "Game_Date='" . $select . "'"; $query = mysql_query("SELECT * FROM june122011 " . $where); $results = mysql_fetch_array($query); While ($results = mysql_fetch_array($query)) { echo "$results[Game_Id]"; } Quote Link to comment https://forums.phpfreaks.com/topic/239454-how-can-you-set-a-unique-variable-for-each-value-in-an-array/#findComment-1230162 Share on other sites More sharing options...
PFMaBiSmAd Posted June 15, 2011 Share Posted June 15, 2011 If you formed the whole query statement in a variable and then echoed it, you could probably see what is causing the query to fail. Quote Link to comment https://forums.phpfreaks.com/topic/239454-how-can-you-set-a-unique-variable-for-each-value-in-an-array/#findComment-1230171 Share on other sites More sharing options...
Failing_Solutions Posted June 15, 2011 Author Share Posted June 15, 2011 Thank you both, redixx that example was great. PFMaBiSmAd after following your advise I was finally able to get the query to execute correctly. I ended up with this in working order. Just a few more things to add now. $where = ''; $range = implode(',',range('A','T')); foreach($_POST['checkbox'] as $checkbox) { $where .= "'" . $checkbox . "' IN (" . $range . ") AND "; } $where .= "Game_Date='" . $select . "'"; $chunk1 ="SELECT * FROM june122011 Where "; $query = $chunk1 . $where; $doit=mysql_query($query); $results=mysql_fetch_array($doit); $num_rows = mysql_num_rows($doit); $adjustedvalue = $num_rows-1; echo "<table><br>You would have won <font color='red'> $adjustedvalue times</font>"; While ($results = mysql_fetch_array($doit)) { echo "<tr><td>$results[GameID]</td></tr>"; } echo "</tabele>"; Many thanks for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/239454-how-can-you-set-a-unique-variable-for-each-value-in-an-array/#findComment-1230244 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.