Table of checkboxes and radio buttons

Like this?

                       <input type="checkbox" name="attend[<?php echo $x; ?>]" value="absence"> 
                    </td> 
                    <td>
                       <input type="checkbox" name="attend[<?php echo $x; ?>]" value="late"> 
                    </td>

gives,

            [4] => 27
            [5] => 29
            [6] => 32
            [7] => 26
        )

    [attend] => Array
        (
            [0] => absence
            [1] => late
            [2] => late
        )

    [equip] => Array
        (
            [0] => 1
            [2] => 1
        )

    [submit] => Submit
)

Now how to get this into the database?
But first I should change the GET to POST as you said earlier.
I’ve got to go to bed now though, 23:14 here.

Thanks for all your help so far,
Shane

Ahh, I forgot to mention changing attend input to a radio button. Here’s another sample with modified form and processing. I also noticed a few short tags in your html e.g. <? from post #29 fixed those as well.

<?php

if(isset($_POST['addnotes'])):
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    
    if(isset($_POST['attend']) || isset($_POST['equip'])):
        include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
        try
        {
        foreach($_POST['student'] as $key => $studentID):
        
            if(array_key_exists($key,$_POST['attend']) || array_key_exists($key,$_POST['equip'])):
            
                $studentid = $_POST['student'][$key];
                $courseid  = $_POST['course'][$key];
                $absence   = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
                $late      = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
                $equip     = (array_key_exists($key,$_POST['equip']) ? '1' : '0'); 
                
                $sql = 'INSERT INTO notes SET
                  userid = :studentid
                , courseid = :courseid
                , date = CURDATE()
                , late = :late
                , absence = :absence
                , equip = :equip';
                $s = $pdo->prepare($sql);
                
                $s->bindParam(':studentid', $studentid);
                $s->bindParam(':courseid', $courseid);
                $s->bindParam(':absence', $absence);
                $s->bindParam(':late', $late);
                $s->bindParam(':equip', $equip);
                $s->execute();
                 
                 
            endif;
            
        endforeach;    
        
        }
        catch (PDOException $e)
        {
            $error = 'Error adding submitted daily notes data. Click the back button to continue.';
            include 'error.html.php';
            exit();
        }
        header('Location: .');
        exit();
    endif;
endif;  
?>

        
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { text-align: center; width: 40px; overflow: hidden;  white-space: nowrap;}
</style>
    <meta charset="utf-8">
    <title>Manage Daily Notes</title>
  </head>
  <body>
      <p><a href="..">physCMS home</a></p>
      <p><a href="?arraytestTWO">array Test</a></p>
      <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
    <h1>Manage Daily Notes</h1>
    <p><a href="?addcourseform">Add Course</a>  &#8658;  
    <?php if(countRows('courses')): ?>
     <a href="?addlearnerform">Add Student</a>
    <?php endif; ?>  
    <?php if(countRows('learners')): ?>
       &#8658;  
       <a href="?courses">Manage Courses and Students</a></p>
    <?php endif; ?>
                 <form action="?addnotes" method="post">
           <table>
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            <th>Late</th>
            <th>Equipment</th>
            </tr>
            <?php $x = 1;?>
            <?php foreach (array_reverse($courses) as $course): 
            foreach (array_reverse($learners) as $learner): ?>
                          <?php if($course['id']==$learner['courseid']): ?>
                     <tr>
                      <td> 
                            <?php htmlout($course['course']) ?>
                      </td>
                        <td>           
                            <?php htmlout($learner['learner']);?> 
                        </td>
                        <td>
                            <input type="hidden" name="course[<?php echo $x; ?>]" value="<?php htmlout($course['id'])?>">
                            <input type="hidden" name="student[<?php echo $x; ?>]" value="<?php htmlout($learner['id']);?>">
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="absence"> 
                        </td> 
                        <td>
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="late"> 
                        </td>
                        <td>
                           <input type="checkbox" name="equip[<?php echo $x; ?>]" value="1"> 
                        </td>
                        </tr>     
                        <?php $x++;?>   
                        <?php endif; ?> 
                <?php endforeach; ?> 
        <?php endforeach; ?>
        </table>
        <input type="submit" name="addnotes" value="Submit"> 
        </form>
</body>
</html>

Hi,
Fantastic it is working now and easy to add new columns of checkboxes.
I just had to remove two endif; from the php file and also there was no courseid column in the table. That’s all fixed now and it works great.
More importantly I do think I understand it now.
I see what you did here also,

$late = (array_key_exists($key,$_POST[‘attend’]) && $_POST[‘attend’][$key] == “late” ? ‘1’ : ‘0’);

If ‘attend’ is set to ‘late’ make the value ‘1’ otherwise make it ‘0’. And similarly for absence.
I thought short php tags were ok, but maybe they are not a good idea.
I really appreciate your help and for taking my through it step by step.
Just in case this any use to anyone I want to post the full codes here.
The html form,

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php';  
      include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php'; 
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildcourses.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildlearners.inc.php';?>
<!DOCTYPE html>
<html lang="en">
  <head>
  <style>
table,th,td
{
border-collapse:collapse;
}
th, td
{
padding:6px;
}
        td { text-align: center; width: 40px; overflow: hidden;  white-space: nowrap;}
</style>
    <meta charset="utf-8">
    <title>Manage Daily Notes</title>
  </head>
  <body>
      <p><a href="..">physCMS home</a></p>
      <p><a href="?arraytestTWO">array Test</a></p>
      <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
    <h1>Manage Daily Notes</h1>
    <p><a href="?addcourseform">Add Course</a>  &#8658;  
    <? if(countRows('courses')): ?>
     <a href="?addlearnerform">Add Student</a>
    <? endif; ?>  
    <? if(countRows('learners')): ?>
       &#8658;  
       <a href="?courses">Manage Courses and Students</a></p>
    <? endif; ?>
       <form action="?addnotes" method="post">
           <table>
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            <th>Late</th>
            <th>Equipment</th>
            </tr>
            <?php $x = 1;?>
            <?php foreach (array_reverse($courses) as $course): 
            foreach (array_reverse($learners) as $learner): ?>
                          <?php if($course['id']==$learner['courseid']): ?>
                     <tr>
                      <td> 
                            <?php htmlout($course['course']) ?>
                      </td>
                        <td>           
                            <?php htmlout($learner['learner']);?> 
                        </td>
                        <td>
                            <input type="hidden" name="course[<?php echo $x; ?>]" value="<?php htmlout($course['id'])?>">
                            <input type="hidden" name="student[<?php echo $x; ?>]" value="<?php htmlout($learner['id']);?>">
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="absence"> 
                        </td> 
                        <td>
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="late"> 
                        </td>
                        <td>
                           <input type="checkbox" name="equip[<?php echo $x; ?>]" value="1"> 
                        </td>
                        </tr>     
                        <?php $x++;?>   
                        <?php endif; ?> 
                <?php endforeach; ?> 
        <?php endforeach; ?>
        </table>
        <input type="submit" name="addnotes" value="Submit"> 
        </form>
  </body>
</html>

The index.php file,

if (isset($_GET['addnotes']))
{
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
          include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
        try
        {
        foreach($_POST['student'] as $key => $studentID)
        {
        
            if(array_key_exists($key,$_POST['attend']) || array_key_exists($key,$_POST['equip'])):
            
                $studentid = $_POST['student'][$key];
                $courseid  = $_POST['course'][$key];
                $absence   = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
                $late      = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
                $equip     = (array_key_exists($key,$_POST['equip']) ? '1' : '0'); 

                $sql = 'INSERT INTO notes SET
                  userid = :studentid
                , courseid = :courseid
                , date = CURDATE()
                , late = :late
                , absence = :absence
                , equip = :equip';
                $s = $pdo->prepare($sql);
                
                $s->bindParam(':studentid', $studentid);
                $s->bindParam(':courseid', $courseid);
                $s->bindParam(':absence', $absence);
                $s->bindParam(':late', $late);
                $s->bindParam(':equip', $equip);
                $s->execute();
            endif;
        }    
        
        }
        catch (PDOException $e)
        {
            $error = 'Error adding submitted daily notes data. Click the back button to continue.';
            include 'error.html.php';
            exit();
        }
        header('Location: .');
        exit();
}

I will post improvements.
Thanks again,
Shane

Perhaps this is too ambitious but I would love the next column for ‘effort’ to have a pull down menu on each row with option from 1 to 5.
I will get thinking about this next and a new thread if and when I get stuck!
Thanks,
Shane

<select name="effort[<?php echo $x; ?>]">
<option value="0">-</option>
<?php
foreach(range(1,5) as $ef):
echo '<option value="'.$ef.'">'.$ef.'</option><br />'."\r";
endforeach;
?>
</select>

Brilliant! That works,

    foreach($_POST['student'] as $key => $studentID)
    {
    
                   if(array_key_exists($key,$_POST['attend']) || array_key_exists($key,$_POST['equip']) || array_key_exists($key,$_POST['effort'])):
        
            $studentid = $_POST['student'][$key];
            $courseid  = $_POST['course'][$key];
            $absence   = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
            $late      = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
            $equip     = (array_key_exists($key,$_POST['equip']) ? '1' : '0'); 
            $effort    = $_POST['effort'][$key];

            $sql = 'INSERT INTO notes SET
              userid = :studentid
            , courseid = :courseid
            , date = CURDATE()
            , time = CURTIME()
            , late = :late
            , absence = :absence
            , effort = :effort
            , equip = :equip';
            $s = $pdo->prepare($sql);
            
            $s->bindParam(':studentid', $studentid);
            $s->bindParam(':courseid', $courseid);
            $s->bindParam(':absence', $absence);
            $s->bindParam(':late', $late);
            $s->bindParam(':equip', $equip);
            $s->bindParam(':effort', $effort);
            $s->execute();
        endif;
    } 

I have to say the array_key_exists() function is really useful.
Wish I could just code like you.
Thank you so much,
Shane

Hi,
I can’t figure out why this keeps adding 8 lines, a row for every row of the table when I only add one item of data, such as tick one checkbox.

if(array_key_exists($key,$_POST['attend']) || array_key_exists($key,$_POST['equip']) || array_key_exists($key,$_POST['effort'])):
            
                $studentid = $_POST['student'][$key];
                $courseid  = $_POST['course'][$key];
                $absence   = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
                $late      = (array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
                $equip     = (array_key_exists($key,$_POST['equip']) ? '1' : '0'); 
                $effort    = $_POST['effort'][$key];

                $sql = 'INSERT INTO notes SET
                  userid = :studentid
                , courseid = :courseid
                , date = CURDATE()
                , time = CURTIME()
                , late = :late
                , absence = :absence
                , effort = :effort
                , equip = :equip';
                $s = $pdo->prepare($sql);
                
                $s->bindParam(':studentid', $studentid);
                $s->bindParam(':courseid', $courseid);
                $s->bindParam(':absence', $absence);
                $s->bindParam(':late', $late);
                $s->bindParam(':equip', $equip);
                $s->bindParam(':effort', $effort);
                $s->execute();
            endif;
        }    

Could the problem be the ternary operator?
How do i get it to check and set a value or else do nothing? So that if the value is not to be set to ‘1’ than don’t set it to anything?
Shane

Not sure without seeing current form. As you’ve added a number of different inputs you might need to check more thoroughly that they are set.

if(isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) || 
isset($_POST['equip']) && array_key_exists($key,$_POST['equip'])  || 
isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) && $_POST['effort'][$key] != 0):

    $studentid = $_POST['student'][$key];
    $courseid  = $_POST['course'][$key];
    $absence   = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
    $late      = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
    $equip     = (isset($_POST['equip']) && array_key_exists($key,$_POST['equip']) ? '1' : '0');
    $effort    = (isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) ? $_POST['effort'][$key] : '0'); 

Hi,
The form is as follows,

<form action="?addnotes" method="post">
           <table>
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            <th>Late</th>
            <th>Equipment</th>
            <th>Effort</th>
            </tr>
            <?php $x = 1;?>
            <?php foreach (array_reverse($courses) as $course): 
            foreach (array_reverse($learners) as $learner): ?>
                          <?php if($course['id']==$learner['courseid']): ?>
                     <tr>
                      <td> 
                            <?php htmlout($course['course']) ?>
                      </td>
                        <td>           
                            <?php htmlout($learner['learner']);?> 
                        </td>
                        <td>
                            <input type="hidden" name="course[<?php echo $x; ?>]" value="<?php htmlout($course['id'])?>">
                            <input type="hidden" name="student[<?php echo $x; ?>]" value="<?php htmlout($learner['id']);?>">
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="absence"> 
                        </td> 
                        <td>
                           <input type="radio" name="attend[<?php echo $x; ?>]" value="late"> 
                        </td>
                        <td>
                           <input type="checkbox" name="equip[<?php echo $x; ?>]" value="1"> 
                        </td>
                        <td>
                            <select name="effort[<?php echo $x; ?>]">
                            <option value="0">-</option>
                            <?php
                             foreach(range(1,5) as $ef):
                             echo '<option value="'.$ef.'">'.$ef.'</option><br />'."\r";
                             endforeach;
                             ?>
                            </select>
                        </td>
                        </tr>     
                        <?php $x++;?>   
                        <?php endif; ?> 
                <?php endforeach; ?> 
        <?php endforeach; ?>
        </table>
        <input type="submit" name="addnotes" value="Submit"> 
        </form>

I see you are using isset() instead of array_key_exists() I will put this in now and see what happens.

Thanks,
Shane

Look more carefully. Using both and also making sure effort != 0

Yes just saw that now, ‘as well as’ and making sure effort is not equal to zero.

That appears to work now. Only adds one row of data.

1 Like

Ok so isset() tells you if a value exists and array_key_exists() tells you if a key exists even if there is no variable or value for that key. That is my understanding.
So here everything is checked for both, that there is a value and a key.

Thanks,
Shane

They both really are keys of POST, e.g. effort and $key number and will only be set if checkbox is selected. If you don’t check for the first then try to check if array_key_exists($key,$_POST['effort']) then you will get an undefined index error for ‘effort’.

Ok makes sense, because I did try array_key_exists($key,$_POST[‘effort’]) on its own and the form page was not displaying, just blank.
It is odd that if there is an error in the php file, the html file will not display, even though the php file is not called until the submit button in the html file is pressed.

Thanks,
Shane

I am trying to add a comment column. But I am getting a similar problem to the one I had before, where when i add a comment 7 other rows are being added also.
I am using,

$comment   = (isset($_POST['comment']) && array_key_exists($key,$_POST['comment']) ? $_POST['comment'][$key] : '0'); 

But I don’t want the comment field to be zero if there is no comment, just leave it empty. Because I am hoping to take this data from the database and put it into a pdf. Therefore i don’t want a ‘0’ for a comment where it should actually be empty. Although I could just tell the function creating the pdf to ignore zeroes in the comment field.

So the only issue i have right now is that multiple rows are being added when I add one comment.

Here are files, notes.html

   <form action="?addnotes" method="post">
           <input type="submit" name="addnotes" value="Submit"> 
       <table>
        <tr>
        <th>Course</th>
        <th>Student</th>
        <th>Absence</th>
        <th>Late</th>
        <th>Equipment</th>
        <th>Effort</th>
        <th>Comment</th>
        </tr>
        <?php $x = 1;?>
        <?php foreach (array_reverse($courses) as $course): 
        foreach (array_reverse($learners) as $learner): ?>
                      <?php if($course['id']==$learner['courseid']): ?>
                 <tr>
                  <td> 
                        <?php htmlout($course['course']) ?>
                  </td>
                    <td>           
                        <?php htmlout($learner['learner']);?> 
                    </td>
                    <td>
                        <input type="hidden" name="course[<?php echo $x; ?>]" value="<?php htmlout($course['id'])?>">
                        <input type="hidden" name="student[<?php echo $x; ?>]" value="<?php htmlout($learner['id']);?>">
                       <input type="radio" name="attend[<?php echo $x; ?>]" value="absence"> 
                    </td> 
                    <td>
                       <input type="radio" name="attend[<?php echo $x; ?>]" value="late"> 
                    </td>
                    <td>
                       <input type="checkbox" name="equip[<?php echo $x; ?>]" value="1"> 
                    </td>
                    <td>
                        <select name="effort[<?php echo $x; ?>]">
                        <option value="0">-</option>
                        <?php
                         foreach(range(1,5) as $ef):
                         echo '<option value="'.$ef.'">'.$ef.'</option><br />'."\r";
                         endforeach;
                         ?>
                        </select>
                    </td>
                    <td>
                    <textarea name="comment[<?php echo $x; ?>]" rows="1" cols="40"></textarea>
                    </td>
                    </tr>     
                    <?php $x++;?>   
                    <?php endif; ?> 
            <?php endforeach; ?> 
    <?php endforeach; ?>
    </table>
    <input type="submit" name="addnotes" value="Submit"> 
    </form>

and index.php

if (isset($_GET['addnotes']))
{
    //echo "<pre>";
    //print_r($_POST);
    //echo "</pre>";
          include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
        try
        {
        foreach($_POST['student'] as $key => $studentID)
        {
            if(isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) || 
isset($_POST['equip']) && array_key_exists($key,$_POST['equip'])  || 
isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) && $_POST['effort'][$key] != 0 ||
isset($_POST['comment']) && array_key_exists($key,$_POST['comment'])):

    $studentid = $_POST['student'][$key];
    $courseid  = $_POST['course'][$key];
    $absence   = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
    $late      = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
    $equip     = (isset($_POST['equip']) && array_key_exists($key,$_POST['equip']) ? '1' : '0');
    $effort    = (isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) ? $_POST['effort'][$key] : '0'); 
    $comment   = (isset($_POST['comment']) && array_key_exists($key,$_POST['comment']) ? $_POST['comment'][$key] : '0'); 
    //$comment  = $_POST['comment'][$key];

                $sql = 'INSERT INTO notes SET
                  userid = :studentid
                , courseid = :courseid
                , date = CURDATE()
                , time = CURTIME()
                , late = :late
                , absence = :absence
                , effort = :effort
                , comment = :comment
                , equip = :equip';
                $s = $pdo->prepare($sql);
                $s->bindParam(':studentid', $studentid);
                $s->bindParam(':courseid', $courseid);
                $s->bindParam(':absence', $absence);
                $s->bindParam(':late', $late);
                $s->bindParam(':equip', $equip);
                $s->bindParam(':effort', $effort);
                $s->bindParam(':comment', $comment);
                $s->execute();
            endif;
        }    
        }
        catch (PDOException $e)
        {
            $error = 'Error adding submitted daily notes data. Click the back button to continue.';
            include 'error.html.php';
            exit();
        }
        header('Location: .');
        exit();
}

Thanks,
Shane

You don’t REALLY need to check if isset($_POST['comment']) && array_key_exists($key,$_POST['comment'])) as this input will always be set and the key will exist but you need to check for !empty($_POST['comment'][$key]) in your wrapping condition.

Same for setting the $comment variable. Do it like you did for $studentid

That did it, works perfectly now.

Just posting the code in case it is useful to anyone,

foreach($_POST['student'] as $key => $studentID)
        {
            if(isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) || 
isset($_POST['equip']) && array_key_exists($key,$_POST['equip'])  || 
isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) && $_POST['effort'][$key] != 0 ||
!empty($_POST['comment'][$key])):

    $studentid = $_POST['student'][$key];
    $courseid  = $_POST['course'][$key];
    $absence   = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "absence" ? '1' : '0');
    $late      = (isset($_POST['attend']) && array_key_exists($key,$_POST['attend']) && $_POST['attend'][$key] == "late" ? '1' : '0');
    $equip     = (isset($_POST['equip']) && array_key_exists($key,$_POST['equip']) ? '1' : '0');
    $effort    = (isset($_POST['effort']) && array_key_exists($key,$_POST['effort']) ? $_POST['effort'][$key] : '0'); 
    $comment  = $_POST['comment'][$key];

                $sql = 'INSERT INTO notes SET
                  userid = :studentid
                , courseid = :courseid
                , date = CURDATE()
                , time = CURTIME()
                , late = :late
                , absence = :absence
                , effort = :effort
                , comment = :comment
                , equip = :equip';
                $s = $pdo->prepare($sql);
                $s->bindParam(':studentid', $studentid);
                $s->bindParam(':courseid', $courseid);
                $s->bindParam(':absence', $absence);
                $s->bindParam(':late', $late);
                $s->bindParam(':equip', $equip);
                $s->bindParam(':effort', $effort);
                $s->bindParam(':comment', $comment);
                $s->execute();
            endif;
        }  

I really do appreciate your help, I’ve learnt a huge amount on this thread alone.
I do have one more issue which i am going to post as a new thread. The problem is that the time is 7 hours behind CET (Central European Time). I have tried adding 7 to it but it’s not working.
Thank you so much,
Shane

You might look at this.
/synchronize-php-mysql-timezone

I have not tried this. buy hey, it’s by SitePoint.

Ok I will definitely look at it,

I have moved to a new thread at

http://www.sitepoint.com/community/t/howto-increase-curtime-by-7-hours/105238

Thanks