Lazarus Rising
Lazarus Rising

Reputation: 2685

access element with variable name

I have a checkbox table declared like this:

 for ($x = 0; $x < 6; $x++) {
                echo "<tr>";
                echo "<td>"; echo $days[$x];  echo '</td>'; //displays days

                       echo '<td><input type="checkbox" name="check_list[]" onClick="toggle(this, '.$x.')" value="1"/> All';
                       echo '<input type="hidden" name="check_list[]" onClick="toggle(this, '.$x.')" value="0"/></td>'; //creates check all buttons

                       for ($y = 0; $y < 12; $y++){
                        echo '<td><input type="checkbox" name="'.$x.'" value="1"> Bar 1<br/></td>';  //creates the other buttons
                        }
                 echo "</tr>";
            } 

The name="check_list[]" checkbox selects all the checkboxes in the same row when checked. It is done with this script:

<script language="JavaScript">
                    function toggle(source, id) {
                            checkboxes = document.getElementsByName(id);
                            for(var i=0, n=checkboxes.length;i<n;i++) {
                                checkboxes[i].checked = source.checked;
                            }
                    }
</script>

Then the data are stored like this in the database:

<?php
        $i=0;       $store[20];     $check[20];
    foreach($_POST['check_list'] as $value){
            $store[$i] =  $value;               $i= $i + 1;
    } 



    $check = str_split(preg_replace('/10/','1',implode('',$store)));
     array_walk($check, 'intval');



    if($check[0]) {      $monday  = 1; }        if(!$check[0]) {$monday  = 2; }                 
    if($check[1]) {      $tuesday  = 1; }       if(!$check[1]) {$tuesday  = 2; }                    
    if($check[2]) {      $wednesday  = 1; }     if(!$check[2]) {$wednesday  = 2; }                  
    if($check[3]) {      $thursday  = 1; }      if(!$check[3]) {$thursday  = 2; }               
    if($check[4]) {      $friday  = 1; }        if(!$check[4]) {$friday  = 2; }                 
    if($check[5]) {      $saturday  = 1; }      if(!$check[5]) {$saturday  = 2; }
?>

Obviously, this is not the whole code (I wrote the sql query to connect and insert the variables for the days. And it worked). But till now I am only taking the values of the first checkbox for each row. Which means that if I check it, all the checkboxes of that row are checked. But I am only reading the fact that all the checkboxes of that row are checked. If I only check a normal checkbox in a row, its value is not posted and I the only info stored is that not all checkboxes are checked (represented by value 2).

What I want to do is take the value of the normal checkboxes for each row instead, and concatenate them in a number. For example, for monday checkboxes (checked, unchecked, unchecked, checked) i would store $monday = 1221. The same must be done for all days (monday to saturday)

Upvotes: 2

Views: 75

Answers (3)

Lazarus Rising
Lazarus Rising

Reputation: 2685

I appreciate the suggestions given, but I actually tried an alternative solution. Instead of referring to the elements by name or id, I refer to them by class and by name. I use the class (variable) to do the toggle function and the name (which is a one dimensional array) to get their values.

In short, this is the general idea of the code: The checkbox declaration

echo '<td><input type="checkbox" class="'.$x.'" name="check_list[]"  value="1"> <br/>';

The toggle function:

function toggle(source, id) {
                            checkboxes = document.getElementsByClassName(id);
                            for(var i=0, n=checkboxes.length;i<n;i++) {checkboxes[i].checked = source.checked;}
}

The php code to get the values:

$i=0;       $unclean[180];
foreach($_POST['check_list'] as $value){
            $unclean[$i] =  $value;             $i= $i + 1;
} 

Then, knowing the dimensions the matrix had, i simply separate this one dimensional array into an array of one dimensional arrays (basically a matrix) iterating through it with a for loop

Upvotes: 0

acontell
acontell

Reputation: 6932

You can do as follows:

for ($y = 0; $y < 12; $y++){
        <input type="text" name="matrix['.$x.']['.$y.']" value="Text" />
}

And in your sever:

foreach($_POST['matrix'] as $key => $value)  {
   echo $key;// Value of $x
   foreach($value as $k => $val) {
        echo $k;// This would be the value of $y
        echo $val;// This would be the value of the inputs
    }
}

Upvotes: 1

Whirl Mind
Whirl Mind

Reputation: 884

The way you describe it, it is not possible. It is not possible to put an array declaration as a variable name. However, I somewhat get (and assume) a picture of what you want to do.

After creating the input elements dynamically, also create a hidden input element that contains the "count" of how many elements where created. Then, in PHP code, first access the count element to find out how many elements are present. Then, loop through that index, to build the loop name for each element and access their values.

Upvotes: 0

Related Questions