OK I'm not really sure how to put it but I will try my best to explain my question. Basically I am really lost when it comes to arrays and for loops, so I need help badly.

I have in my HTML Form about 20 checkboxes like this:

echo "<input type='checkbox' 
            name='pro_id[{$row['product_id']}]' 
            value='Box #{$row['product_id']}' checked>";

The values product_id is taken from my database.
When the user submits, it'll run:

foreach($_POST['pro_id'] as $_id => $_label) 
{chunkofcodes}

But now, what I need to do is to actually limit the $_POST and group them by 7s, and run that "chunkofcodes" using the first 7 values, the next 7 values, and the remainder of the values, in this case 20 - 7 - 7 = 6. Please note that the number of checkboxes is not permanently 20; it may even be more.

So if I'm not wrong, I'm supposed to work with for loops, right? But I am really lost.

first, how do i "group" the values in an array of 7?
next, how do i use that array to run the "chunkofcodes" one after another?

Recommended Answers

All 4 Replies

<?php 
if (isset($_POST['next'])) {
	
	/*
	 * In order to see how your posted data looks like, use the following statement.
	 * print_r($_POST['porid']);
	 */
	
	
	/*
	 * Type 1
	 * Say you have 20 checkboxes and you have selected only 8 out of them, then only these 8 checkbox values will be posted.
	 * Now if you want to group only these 8 selected checkbox values into a group of 7 then take the snippet till -- Type 1 Ends --
	 */
	$group_limit = 7; // Number of elements in a group.
	$chk2d = array(); // The array which holds the posted values. This will be a 2 dimensional array.
	// $group_count is the number of groups required.
	$group_count = (count($_POST['proid'])%$group_limit > 0) ? intval(count($_POST['proid'])/$group_limit) + 1 : count($_POST['proid'])/$group_limit;
	for ($j = 0; $j < $group_count; $j++) {
		for ($k=0; $k < $group_limit; $k++) {
			$index = ($j*$group_limit) + $k;
			if (isset($_POST['proid'][$index])) {
				$chk2d[$j][$k] = $_POST['proid'][$index];
			}
		}
	}
	print "<pre>Type 1";
	print_r($chk2d);
	print "</pre>";
	
	/********* Type1 Ends ********/
	
	/*
	 * Type 2
	 * Say you want to group all the 20 checkboxes irrespective of the what checkboxes you have selected, then the following code works.
	 * In this case you will need an extra 'hidden' input type in the form which will post you the number of checkboxes details.
	 */
	$group_limit = 7; // Number of elements in a group.
	$chk2d = array(); // The array which holds the posted values. This will be a 2 dimensional array.
	$selected_porduct_id = array();
	foreach ($_POST['proid'] as $key => $value) {
		$selected_porduct_id[] = substr($value,strpos($value,"#")+1);
	}
	// $group_count is the number of groups required.
	$group_count = ($_POST['chkbox_count']%$group_limit > 0) ? intval($_POST['chkbox_count']/$group_limit) + 1 : $_POST['chkbox_count']/$group_limit;
	for ($j = 0; $j < $group_count; $j++) {
		for ($k=0; $k < $group_limit; $k++) {
			$chk_index = ($j*$group_limit) + $k;
			if (in_array($chk_index,$selected_porduct_id)) {
				$key_array = array_keys($selected_porduct_id,$chk_index);
				$index = $key_array[0];
				$chk2d[$j][$k] = $_POST['proid'][$index];
			} else {
				$chk2d[$j][$k] = "";
			}
		}
	}
	print "<pre>Type 2";
	print_r($chk2d);
	print "</pre>";
	
	/********* Type2 Ends ********/
	
	
	// Now you can process the $chk2d array as per your requirement.
}
$x = 20; // Number of checkboxes.
$htmloutput = "<form name='chkbox_form' action='test1.php' method='post'>";
$htmloutput .= "<input type='hidden' name='chkbox_count' value='$x' >";
for ($i = 0; $i < 20; $i++) {
	$htmloutput .= "<input type='checkbox' name='proid[]' value='Box#$i'> $i <br />";
}
$htmloutput .= "<input type='submit' name='next' value='Next'>";
$htmloutput .= "</form>";
echo $htmloutput;
?>

what i have tried to do is just format the posted data of the checkboxes into a 2- dimensional array. After doing that you can easily apply the foreach() loop on that 2-dimensional array and then process it the way you want.

The format that i have tried to accomplish is like..

$array[group_number][selected box];

so it will look like..

Group 1 ..
$array[0][0]
$array[0][1]
$array[0][2]
$array[0][3]
$array[0][4]
$array[0][5]
$array[0][6]


Group 2 ..
$array[1][0]
$array[1][1]
$array[1][2]
$array[1][3]
$array[1][4]
$array[1][5]
$array[1][6]


I hope that helps..
( Also read the comments in the code )

That is awesome! But there are a few flaws in the result that I need to show you.

So here's my codes with a little tweaks to you Type 1:

$pro_ct=0;

foreach($_POST['pro_id'] as $_id => $_label) 
{
$pro_ct++;
}

$group_limit = 7;

$chk2d = array();

$group_count = ($pro_ct % $group_limit > 0) ? intval($pro_ct / $group_limit) + 1 : $pro_ct / $group_limit;

for ($j=0; $j < $group_count; $j++) 
{
	for ($k=0; $k <= $group_limit; $k++) 
	{
		$index = ($j * $group_limit) + $k;

		if (isset($_POST['pro_id'][$index])) 
		{
			$chk2d[$j][$k] = $_POST['pro_id'][$index];
		}
	}
}

print "<pre>Type 1";
print_r($chk2d);
print "</pre>";

Instead of 20, now I am working with only 12 checkboxes. I have a few scenarios that I tested:

All checked:
[v] item 1
[v] item 2
[v] item 3
[v] item 4
[v] item 5
[v] item 6
[v] item 7
[v] item 8
[v] item 9
[v] item 10
[v] item 11
[v] item 12

Result (Box 7 appears twice?):

Type 1Array
(
    [0] => Array
        (
            [1] => Box #1
            [2] => Box #2
            [3] => Box #3
            [4] => Box #4
            [5] => Box #5
            [6] => Box #6
            [7] => Box #7
        )

    [1] => Array
        (
            [0] => Box #7
            [1] => Box #8
            [2] => Box #9
            [3] => Box #10
            [4] => Box #11
            [5] => Box #12
        )

)

1-6 checked:
[v] item 1
[v] item 2
[v] item 3
[v] item 4
[v] item 5
[v] item 6
[ ] item 7
[ ] item 8
[ ] item 9
[ ] item 10
[ ] item 11
[ ] item 12

result (looks perfect):

Type 1Array
(
    [0] => Array
        (
            [1] => Box #1
            [2] => Box #2
            [3] => Box #3
            [4] => Box #4
            [5] => Box #5
            [6] => Box #6
        )

)

1-5 unchecked:
[ ] item 1
[ ] item 2
[ ] item 3
[ ] item 4
[ ] item 5
[v] item 6
[v] item 7
[v] item 8
[v] item 9
[v] item 10
[v] item 11
[v] item 12

result:

Type 1Array
(
    [0] => Array
        (
            [6] => Box #6
            [7] => Box #7
        )

)

I wanted it to have box 6 - 12 in array [0]

last one:
4,5,10,12 unchecked:
[v] item 1
[v] item 2
[v] item 3
[ ] item 4
[ ] item 5
[v] item 6
[v] item 7
[v] item 8
[v] item 9
[ ] item 10
[v] item 11
[ ] item 12

result:

Type 1Array
(
    [0] => Array
        (
            [1] => Box #1
            [2] => Box #2
            [3] => Box #3
            [6] => Box #6
            [7] => Box #7
        )

    [1] => Array
        (
            [0] => Box #7
            [1] => Box #8
            [2] => Box #9
            [4] => Box #11
        )

)

I am pretty lost after doing these test... can you guide me from here?

Btw thank you so much for the clear and elaborate answer... really appreciate it! =)

Here is the changed code for Type 1...

/*
	 * Type 1
	 * Say you have 20 checkboxes and you have selected only 8 out of them, then only these 8 checkbox values will be posted.
	 * Now if you want to group only these 8 selected checkbox values into a group of 7 then take the snippet till -- Type 1 Ends --
	 */
	$group_limit = 7; // Number of elements in a group.
	$chk2d = array(); // The array which holds the posted values. This will be a 2 dimensional array.
	// $group_count is the number of groups required.
	$pro_ct = 0;
	foreach($_POST['proid'] as $_id => $_label) {
		$pro_ct++;
	}
	//$group_count = (count($_POST['proid'])%$group_limit > 0) ? intval(count($_POST['proid'])/$group_limit) + 1 : count($_POST['proid'])/$group_limit;
	$group_count = ($pro_ct%$group_limit > 0) ? intval($pro_ct/$group_limit) + 1 : $pro_ct/$group_limit;
	$j = 0;
	$k = 0;
	
	foreach($_POST['proid'] as $_id => $_label) {
		$pro_ct++;
		$chk2d[$j][$k] = $_label;
		$k++;
		if ($k >= $group_limit) {
			$k = 0;
			$j++;
		}
	}
	
	print "<pre>Type 1";
	print_r($chk2d);
	print "</pre>";

I hope that helps :)

Omg that's perfect! Thank you so much! To continue with my program, I will have to do more tests first, so for now I will still leave the thread open since I may have some more questions coming up! Thank you once again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.