yariv bar
yariv bar

Reputation: 976

guarantee php include/require in the end of file

in my web app there's this page:

<?php require_once('head.php');?>
<?php require_once('nav.php');?>


<div class="container-fluid">

    <div class="container EXAMPLE">

            <?php
                if(isset($_SESSION['lecturer'])){
                    require_once('lecturer-courses.php');
                } else{
                    require_once('student-courses.php');
                }
            ?>

    </div>

</div>

<?php require_once('footer.php') ?>

each one of the lecturer-courses.php/student-courses.php files creates a table with lots of data and involves some server work and communication with DB. the problem is that the footer html code always end up inside the the div with the EXAMPLE class. I'm assuming that it has something to do with the time it takes for the tables to be created. any idea how can i make sure the footer will only be required after any other earlier required code? thx

UPDATE: student-courses.php:

<?php
$output = '<h2>My Courses</h2>';
$output .= '<table class="table">';
$output .=  '<thead>';
$output .=  '<tr>';
$output .=  '<th>Course</th>';
$output .=  '<th>Lecturer</th>';
$output .=  '<th>Email</th>';
$output .=  '<th>Day Limit</th>';
$output .=  '</tr>';
$output .=  '</thead>';
$output .=  '<tbody>';

echo $output;

    $courses = $auth_user->getCourses($userRow["user_id"]);
    foreach($courses as $course){
      $row = '<tr>';
      $row .= '<td><a href="course.php?cid='.$course["course_id"].'">'.$course["course_name"].'</a></td>';
      $row .= '<td>'.$course["first_name"]." ".$course["last_name"].'</td>';
      $row .= '<td>'.$course["email"].'</td>';
      $row .= '<td>'.$course["day_limit"].'</td>';
      $row .= '<tr>';
      echo $row;
    }

$output .= '</tbody>';
$output .= '</table>';

?>

Upvotes: 1

Views: 66

Answers (1)

bluepinto
bluepinto

Reputation: 165

in your student-courses.php code, the last two lines also need to be echoed out.

$output .= '</tbody>';
$output .= '</table>';

To show/prevent the footer you can include some logic in lecturer-courses.php and or student-courses.php, as your requirement is;

if(condition met)$footer_disp = FALSE;
else $footer_disp = TRUE;

then in your main page;

<?php 
if(!empty($footer_disp))require_once('footer.php') 
?>

Upvotes: 1

Related Questions