Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

matrix, not checked

<?php // RAY_temp_rgb192a.php
error_reporting(E_ALL);

// SHOW THE LOCATION OF THE SELECTION IN THE MATRIX
if (!empty($_POST))
{
    foreach ($_POST as $key => $val)
    {
        $arr = explode('_', $key);
        $row = str_replace('R', NULL, $arr[0]);
        $col = str_replace('C', NULL, $arr[1]);
        echo "<br/>YOU CHOSE ROW $row AND COLUMN $col" . PHP_EOL;
    }
}

$html = <<<ENDHTML
<form method="post">
Choose a Checkbox or Two
<table>
<tr>
<td><input type="checkbox" value="1" name="R1_C1" /></td>
<td><input type="checkbox" value="1" name="R1_C2" /></td>
<td><input type="checkbox" value="1" name="R1_C3" /></td>
</tr>
<tr>
<td><input type="checkbox" value="1" name="R2_C1" /></td>
<td><input type="checkbox" value="1" name="R2_C2" /></td>
<td><input type="checkbox" value="1" name="R2_C3" /></td>
</tr>
<tr>
<td><input type="checkbox" value="1" name="R3_C1" /></td>
<td><input type="checkbox" value="1" name="R3_C2" /></td>
<td><input type="checkbox" value="1" name="R3_C3" /></td>
</tr>
</table>
<input type="submit" />
</form>
ENDHTML;

echo $html;

Open in new window


matrix
this code tells me what checkboxes have been checked
so
please tell me what checkboxes have been checked AND NOT been checked

I intend on making this matrix larger
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

you can just add hidden inputs before the checkbox in the HTML with a value of zero like this:

<?php // RAY_temp_rgb192a.php
error_reporting(E_ALL);

// SHOW THE LOCATION OF THE SELECTION IN THE MATRIX
if (!empty($_POST))
{
    foreach ($_POST as $key => $val)
    {
            if ( $val == 1 ) {
                  $arr = explode('_', $key);
                  $row = str_replace('R', NULL, $arr[0]);
                  $col = str_replace('C', NULL, $arr[1]);
                  echo "<br/>YOU CHOSE ROW $row AND COLUMN $col" . PHP_EOL;
            } else {
                  echo "<br/>YOU DIDNT CHOSE ROW $row AND COLUMN $col" . PHP_EOL;
            }
    }
}

$html = <<<ENDHTML
<form method="post">
Choose a Checkbox or Two
<table>
<tr>
<td><input type="hidden" name="R1_C1" value="0" /><input type="checkbox" value="1" name="R1_C1" /></td>
<td><input type="hidden" name="R1_C2" value="0" /><input type="checkbox" value="1" name="R1_C2" /></td>
<td><input type="hidden" name="R1_C3" value="0" /><input type="checkbox" value="1" name="R1_C3" /></td>
</tr>
<tr>
<td><input type="hidden" name="R2_C1" value="0" /><input type="checkbox" value="1" name="R2_C1" /></td>
<td><input type="hidden" name="R2_C2" value="0" /><input type="checkbox" value="1" name="R2_C2" /></td>
<td><input type="hidden" name="R2_C3" value="0" /><input type="checkbox" value="1" name="R2_C3" /></td>
</tr>
<tr>
<td><input type="hidden" name="R3_C1" value="0" /><input type="checkbox" value="1" name="R3_C1" /></td>
<td><input type="hidden" name="R3_C2" value="0" /><input type="checkbox" value="1" name="R3_C2" /></td>
<td><input type="hidden" name="R3_C3" value="0" /><input type="checkbox" value="1" name="R3_C3" /></td>
</tr>
</table><input type="submit" />
</form>
ENDHTML;

echo $html;
Expanding on rays example, you could create your matrix like so:

<?php

// RAY_temp_rgb192a.php
error_reporting(E_ALL);

// SHOW THE LOCATION OF THE SELECTION IN THE MATRIX
if (!empty($_POST))
{
    foreach ($_POST as $key => $val)
    {
            if ( $val == 1 ) {
                  $arr = explode('_', $key);
                  $row = str_replace('R', NULL, $arr[0]);
                  $col = str_replace('C', NULL, $arr[1]);
                  echo "<br/>YOU CHOSE ROW $row AND COLUMN $col" . PHP_EOL;
            } else {
                  echo "<br/>YOU DIDNT CHOSE ROW $row AND COLUMN $col" . PHP_EOL;
            }
    }
}
$layout = array("rows" => array('R1', 'R2', "R3"), "columns" => array('C1', "C2", "C3"));
$op = "<table>";
foreach ($layout['rows'] as $row) {
    $op.="<tr>";
    foreach ($layout['columns'] as $column) {
        $op.= '<td><input type="checkbox" value="1" name="' . $row . '_' . $column . '" /></td>';
    }
    $op.="</tr>";
}
$op.="</table>";
$html = <<<ENDHTML
<form method="post">
Choose a Checkbox or Two
{$op}
<input type="submit" />
</form>
ENDHTML;

echo $html;

Open in new window

When text values are left blank in HTML form input, they are empty().  But checkbox and radio buttons that are not fired are unset() in the request.  So your idea to find the ones that are unchecked is going to have you looking for something that does not exist.  In other words, you have to know in advance what checkboxes are potentially in the request.  Fortunately, since your PHP code can generate the HTML forms, you can know this information.

There are two ways of dealing with the issue.  One way is to have an array of expected elements.  Remove all of the checked elements from the array, and that is the set of unchecked elements.  The other way is to create hidden form input elements to represent every checkbox.  The hidden input controls must have exactly the same name as the checkbox controls and a value (perhaps zero) that indicates "unchecked" data.  Put these hidden input controls into the HTML form before the checkboxes.  As checkboxes are fired, the use of the same name= attribute causes the checkbox value to overwrite the hidden value in the request array.
Any way this can all be done with arrays:

<?php
error_reporting(E_ALL);

// SHOW THE LOCATION OF THE SELECTION IN THE MATRIX
if (isset($_POST)) {
    echo "<pre>";
    print_r($_POST);
    echo "<pre/>";
}

$columns = rangePrepend("C",1,8);
$rows = rangePrepend("R",1,9);
$layout = array_fill_keys($rows, $columns);

$op = "<table>";
foreach ($layout as $rk => $rv) {
    $op.="<tr>";
    foreach (array_values($rv) as $ck) {
        $checked = (isset($_POST) && $_POST['layout'][$rk][$ck] == 'on') ? "checked='checked'" : '';
        $op.= '<td><input type="checkbox"  name="layout[' . $rk . '][' . $ck . ']" ' . $checked . '/></td>';
    }
    $op.="</tr>";
}
$op.="</table>";
$html = <<<ENDHTML
<form method="post">
Choose a Checkbox or Two
{$op}
<input type="submit" />
</form>
ENDHTML;

echo $html;

function rangePrepend($p,$s,$e){
    $starter = range($s,$e);
    $rtn=array();
    foreach($starter as $value){        
        $rtn[]=$p.$value;
    }
    return $rtn;   
}

Open in new window

Any you can use iteration to get your results
ASKER CERTIFIED SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

I liked this array example the best
thanks