Submit button and 'id' values problem for a form

This puts a zero in the ‘userid’ field of the table. Which I presume is because this is a number and not string! What about passing users name instead of their id?

question: how do you access the array keys in a foreach() loop?

Hmm, I think this is wrong:


                       <tr> 
                        <? if($course['id']==$learner['courseid']):  

Swap those two lines around - we shouldn’t open the table row unless the learners course id matches the current course id. The closing table row tag is in the right place.

If that doesn’t help, post your code for the loop. Obviously you’ll have to either echo the ‘<tr>’ or put a close-php tag and open tag around it.

Hi,

Drummin: Didn’t know there was such thing as short tags, the <? instead of <?php was just a typo. I have corrected it now, Thanks.

Droopsnoot: I tried changing the lines around, but no luck!

 
                 <form action="?addnotes" method="post">
            <table>
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): 
            $firstcourse = true; // set a flag each time we have a new course id
            foreach (array_reverse($learners) as $learner): ?>
                     
                        <?php if($course['id']==$learner['courseid']):
                  	echo '<tr>';
                        if ($firstcourse) {
                            $firstcourse = false; // so we only display the first column once
                            echo '<td>' . htmlout($course['course']) . '</td>';
                            }
                        else {
                            echo '<td>&nbsp;</td>'; } // and display an empty field in the first column for second and subsequent rows
                            ?>
                        <td>
                            <?php htmlout($learner['learner']);?>             
                            <?php htmlout($learner['id']);?>          
                        </td>
                        <td>
                           <!-- <input type="hidden" name="id[]" id="<?php htmlout($learner['id']);?>" value="<?php htmlout($learner['id']);?>"> -->
                           <!-- <input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1"> -->
                           <?php printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); ?> 
                        </td>
                        <?php endif; ?> 
                        </tr>
                                           
                <?php endforeach; ?>    
        <?php endforeach; ?>
        </table>
        <input type="submit" value="Submit"> // this either needs to be in a table cell, or completely outside the table
        </form>
 
  </body>
</html>

Dormilich: I thought I knew something about arrays but obviously I haven’t a clue :frowning:

how do you access the array keys in a foreach() loop?

I honestly don’t know!!!
Need to go through loops again in codecademy, I think.
Thanks,
Shane

I think all courses should be output but only the learners on that course should be output. So the condition,

                        <?php if($course['id']==$learner['courseid']):

should only apply to the learners.
Shane

http://php.net/foreach

These two lines at the end of the loop are the wrong way around now - you’ve swapped them around from the sample code above:


                        <?php endif; ?> 
                        </tr>

Because you only output the table row opening tag if the if() conditional is true, you must only output the closing tag within that conditional as well, otherwise you’ll have a table row closing tag for every time you go around the loop, regardless of whether you output anything - in this case you’ll have a closing table row tag for every learner… Swap those two lines around, so that the closing tag is before the endif statement, and it should balance.

The loop as it stands will only output courses that have learners. You loop around each course, then output a row for each learner on that course. Because we moved the part where the course information is displayed into the conditional, this only happens for the first learner on that course and therefore if there are no learners on a course, the course id will not be displayed. Are you saying that you want to see all courses, even if they have no learners?

Thanks Dormilich,
I am on it :slight_smile:
Shane

ahh! The link Dormilich just posted has disappearned!

foreach php loops

Hi,
The table is still not behaving! The courses are along the top.

                 <form action="?addnotes" method="post">
            <table>
            <tr>
            <th>Course</th>
            <th>Student</th>
            <th>Absence</th>
            </tr>
            <?php foreach (array_reverse($courses) as $course): 
            $firstcourse = true; // set a flag each time we have a new course id
            foreach (array_reverse($learners) as $learner): ?>
                     
                        <?php if($course['id']==$learner['courseid']):
                  	echo '<tr>';
                        if ($firstcourse) {
                            $firstcourse = false; // so we only display the first column once
                            echo '<td>' . htmlout($course['course']) . '</td>';
                            }
                        else {
                            echo '<td>&nbsp;</td>'; } // and display an empty field in the first column for second and subsequent rows
                            ?>
                        <td>
                            <?php htmlout($learner['learner']);?>             
                            <?php htmlout($learner['id']);?>          
                        </td>
                        <td>
                           <!-- <input type="hidden" name="id[]" id="<?php htmlout($learner['id']);?>" value="<?php htmlout($learner['id']);?>"> -->
                           <!-- <input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1"> -->
                           <?php printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); ?> 
                        </td>
                        </tr>
                    <?php endif; ?>                    
                <?php endforeach; ?>    
        <?php endforeach; ?>
        </table>
        <input type="submit" value="Submit"> 
        </form>

Well actually it is behaving perfectly… it just has a bad master :slight_smile:
Thanks,
Shane

Sorry, is it working or not working now? If I create a page with the code above in it (well, I’m removing htmlout() references) and add some manually-created arrays that have the elements you reference, it displays correctly in IE and Chrome for me.

Hi,
I can’t believe that it is almost two months since I last worked on this problem.
Droopsnoot, thanks for your reply. I tried commenting out the line that calls the htmlout() function, and have now replaced it with blah this,

<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>
        </tr>
        <?php foreach (array_reverse($courses) as $course): 
        $firstcourse = true; // set a flag each time we have a new course id
        foreach (array_reverse($learners) as $learner): ?>
                 <tr>
                 <td> blah </td>
                     <!-- <td> <?php htmlout($course['course']) ?></td> -->
                      <?php if($course['id']==$learner['courseid']): ?>
                    <td>           
                        <?php htmlout($learner['learner']);?>      
                        <?php htmlout($learner['id']);?>          
                    </td>
                    <td>
                       <!-- <input type="hidden" name="id[]" id="<?php htmlout($learner['id']);?>" value="<?php htmlout($learner['id']);?>"> -->
                       <!-- <input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1"> -->
                       <?php printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); ?> 
                    </td>
                                        <?php endif; ?>   
                    </tr>
             ![|471x500](upload://yGDQ9adUPs6aoT4fSqCe30r77Ju.JPG) 
            <?php endforeach; ?>    
    <?php endforeach; ?>
    </table>
    <input type="submit" value="Submit"> 
    </form>

Included is an image of how the table looks. I can’t figure out why the ‘blah’ is repeating.
Thanks,
Shane

I can’t see the image you refer to, but it seems that you’re outputting the ‘blah’ table cell at the start of each row as it’s not enclosed in the if() statement.

Hi,
Thanks for the reply. I have just figured it out and then read your comment. That line does need to be inside the if() statement and it works fine now.

<?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>
      <?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>
            </tr>
            <?php foreach (array_reverse($courses) as $course): 
            $firstcourse = true; // set a flag each time we have a new course id
            foreach (array_reverse($learners) as $learner): ?>
                     <tr>
                     <!-- <td> blah </td> -->
                        
                          <?php if($course['id']==$learner['courseid']): ?>
                      <td> <?php htmlout($course['course']) ?></td>
                        <td>           
                            <?php htmlout($learner['learner']);?>      
                            <?php htmlout($learner['id']);?>          
                        </td>
                        <td>
                           <!-- <input type="hidden" name="id[]" id="<?php htmlout($learner['id']);?>" value="<?php htmlout($learner['id']);?>"> -->
                           <!-- <input type="checkbox" name="absence[]" id="<?php htmlout($learner['id']);?> " value="-1"> -->
                           <?php printf('<input type="checkbox" name="absence[%1$d]" id="absence_%1$d" value="-1">', $learner['id']); ?> 
                        </td>
                                            <?php endif; ?>   
                        </tr>
                 
                <?php endforeach; ?>    
        <?php endforeach; ?>
        </table>
        <input type="submit" value="Submit"> 
        </form>
  </body>
</html>

Thanks,
Shane