...

View Full Version : checkboxes, counting them on submit



masterofollies
05-04-2009, 06:24 PM
So I am building a roulette game, a very simple one where it has 35 numbers (35 checkboxes) and like if you select one number and get it, you win the biggest prize, and if you select 3 its smaller, and like if you select 20 numbers, you get a little prize.

So anyways how do I know which numbers were checked, and how many in a single submit? I looked at this code


Pizza:<input type="checkbox" value="Pizza" name="food[]">

do I make the array the same on all of them? but say I check #7, #12 and #33. How do I do it on the $_POST those numbers, and for it to count 3? So 1 = 50 coins, 3 X 50 = 150 coin entry

Fou-Lu
05-04-2009, 06:37 PM
Thats what the value is. Checkboxes are unique in that they will provide no data if nothing is checked.


<input type="checkbox" value="1" name="guess[]" />
<input type="checkbox" value="2" name="guess[]" />

$_POST / $_GET will contain an array for 'guess'. If you count $_POST['guess'] it will tell you how many selections were made, and each item in the $_POST['guess'] will correspond to the selected number.
Its actually been awhile since I've used a checkbox with multidimensional selections. I'm about 95% certain that its sequentially index, so if you check only the second box, it will have a value like so:


//print_r($_POST);
Array
(
'guess' => Array(
0 => '2'
);
)

masterofollies
05-04-2009, 08:25 PM
I kind of understand, I've never done arrays before, they are extremely confusing.

kbluhm
05-04-2009, 09:36 PM
Fou-Lou is correct. If a box is not checked, it does not exist in the $_POST array.

masterofollies
05-05-2009, 12:18 AM
Yeah but what about this array thing?

Fou-Lu
05-05-2009, 01:05 AM
I don't quite understand what you're question is.
Consider:


$aChecked = (isset($_POST['food']) && is_array($_POST['food'])) ? $_POST['food'] : array();
$iItems = count($aChecked); // Contains the number of items checked
foreach ($aChecked AS $checked)
{
printf("%s\n", $checked); // Lists each of the items checked.
}
// or
print implode("\n", $aChecked);


That make sense?

Fumigator
05-05-2009, 01:24 AM
I think what will help you understand how a bunch of checkboxes with the name of "guess[]" gets transformed into the $_POST array is print_r the $_POST array. Use the <pre> tag to make the output readable.



echo "<pre>".print_r($_POST,true)."</pre>";


See what the output looks like if you check all boxes, check no boxes, and check a few boxes. You should get a pretty good idea of what's going on with $_POST by doing this.

masterofollies
05-05-2009, 05:09 PM
I don't quite understand what you're question is.
Consider:


$aChecked = (isset($_POST['food']) && is_array($_POST['food'])) ? $_POST['food'] : array();
$iItems = count($aChecked); // Contains the number of items checked
foreach ($aChecked AS $checked)
{
printf("%s\n", $checked); // Lists each of the items checked.
}
// or
print implode("\n", $aChecked);


That make sense?

Try that and it does pull the numbers, however as you know I use a page array so all the content is in a certain box. This won't work with printf. When I use printf it acts as an error and displays at the top of the screen.

Len Whistler
05-06-2009, 01:22 AM
Here is something that might be close to what you want. DEMO: http://www.stubby.ca/test.php



<?php

if($_POST['food']) {

$food_array = $_POST['food'];
$counter = "0";

foreach ($food_array as $value) {
$counter++;
echo "Check box number $value checked<br>";
}
echo "<br>$counter Check boxes checked<br><hr>";

}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
<?php

//add as many check boxes to array as you need.
$array = array(1 => 'Pizza','Hotdog','Hamburger','Cheeseburger');

foreach($array as $key => $value) {
echo "$key <input type=\"checkbox\" name=\"food[]\" value=\"$key\">$value</input><br>\n";
}
?>
<br><input type="submit" name="submit" value="Submit">
</form>





-----------

masterofollies
05-06-2009, 03:34 PM
That may work, I will try that.

masterofollies
05-07-2009, 11:14 PM
Sweet it works, but how is it possible to get like 7 numbers in a row across and then start 8 to 15 below that? So it would be like


<td>number 1</td><td>number 2</td> etc.

I tried that and it kept giving me the same numbers over.

Here is my coding.


echo '<table width="95%" align="center">';
$array = array(1 => '1','2','3','4','5');

foreach($array as $key => $value) {
echo "<tr><td><input type=\"checkbox\" name=\"num[]\" value=\"$key\">$value</input></td></tr>";
}

Fumigator
05-07-2009, 11:26 PM
You can use the modulus operator to start a new <tr> row every nth iteration.


<table><tr>
<?php
for ($i = 0; $i < 22; $i++) {
if ($i % 7 == 0 && $i > 1) {
echo "</tr><tr>";
}
echo "<td>$i</td>\n";
}

?>
</tr></table>


Something like that. You'll need to incorporate your array if you want to, otherwise just add 1 to $i to get the checkbox value.

(edit) messed something up.

masterofollies
05-08-2009, 01:24 AM
Hmm really confusing, don't quite understand. Even doing PHP for 3 years, still lack much knowledge. Anyway you can break it down a little or comment what is doing what?

Len Whistler
05-08-2009, 02:07 AM
I updated my original code with some added features and comments that should help. . DEMO: http://www.stubby.ca/test_two.php


<?php

if($_POST['output']) {

$output = $_POST['output'];
$counter = 0;

foreach ($output as $value) {
$counter++;
echo "Check box number $value checked<br>";
}
echo "<br>$counter Check boxes checked<br><hr>";

}
?>

<form method="post" action="<?php echo $PHP_SELF;?>">
<?php

echo "<table><tr>";
$array = range(1, 35); // Range 1-35 checkboxes.
$counter = 0;
$number_of_columns = 7;

foreach($array as $value) {
$counter = ($counter % $number_of_columns); // Study modulus operator.

// When counter equals 0 new row every 7th column.
if ($counter == 0) {
echo "</tr><tr>";
}

echo "<td><input type=\"checkbox\" name=\"output[]\" value=\"$value\">$value</input></td>\n";
$counter++;
}
echo "</tr></table>";
?>
<br><input type="submit" name="submit" value="Submit">
</form>

masterofollies
05-08-2009, 02:22 AM
Excellent thank you!! :thumbsup:

masterofollies
05-08-2009, 02:32 AM
Wait it's not working for me. All 35 of them go across the screen expanding it. I did it exactly as you did (converted into my coding) what is wrong?


if (isset($_POST['play'])) {
$cost = 50;
$output = $_POST['output'];
$counter = 0;

foreach ($output as $value) {
$counter++;
$page .= "Check box number $value checked<br>";
}
$page .= "<br>$counter Check boxes checked<br><hr>";
$page .= '<br /><br /><br /><a href="roulette.php">Return to Roulette</a>';

}
else {
$page .= 'Welcome to the Roulette Game!<br /><br />';
$page .= 'Check each box below for every number you wish to play. Every checked box costs 50 gold. The more you check the better your chances, but the lower your winning amount.<br /><br />';
$page .= '<form action="roulette.php" method="POST">';
$page .= '<table width="95%" align="center"><tr>';
$array = range(1, 35); // Range 1-35 checkboxes.
$counter = 0;
$number_of_columns = 7;

foreach($array as $value) {
$counter = ($counter % $number_of_columns); // Study modulus operator.
// When counter equals 0 new row every 7th column.
if ($counter == 0) {
echo "</tr><tr>";
}
$page .= "<td><input type=\"checkbox\" name=\"output[]\" value=\"$value\">$value</input></td>\n";
$counter++;
}
$page .= '</tr></table><br /><br />';
$page .= '<center><input type="submit" name="play" value=" PLAY ROULETTE! "></form></center>';

Len Whistler
05-08-2009, 03:01 AM
You need one echo for this:


$page .= '</tr></table><br /><br />';
$page .= '<center><input type="submit" name="play" value=" PLAY ROULETTE! "></form></center>';


-----------

masterofollies
05-08-2009, 04:06 AM
If I use an echo it will mess it up. The $page keeps it in a certain div



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum