I have a quandary. I created a custom function(f_TermLevelStatList()) to build and display a high level stats table. Instead of clogging my main PHP file (class_list.php), I built it in a separate file (func_highlevel_statlist.php) that I am including via include_once.
If I run the func_highlevel_statlist.php file in the browser by calling the function f_TermLevelStatList() it works perfectly and displays the table.
When I run the class_list.php file I get the following error message.
Fatal error: Uncaught Error: Call to a member function query() on null in /volume1/web/gh-web/func_highlevel_statlist.php:5 Stack trace: #0 /volume1/web/gh-web/func_highlevel_statlist.php(46): f_TermLevelStatList() #1 /volume1/web/gh-web/class_list.php(2): include_once(’/volume1/web/gh…’) #2 {main} thrown in /volume1/web/gh-web/func_highlevel_statlist.php on line 5
How can I get this to work via include_once?
here is the code of class_list.php:
<?php include_once "./gh_config/db_conn.php";
include_once "func_highlevel_statlist.php";
$sqlListSubCatDesc =
"SELECT DISTINCT `subject`, `catalog`, `description` FROM barpsc.class_tbl " .
" WHERE `subject` Not IN ('PERM') Order By `subject` ASC, `catalog` ASC;";
?>
<!DOCTYPE HTML5>
<html>
<head>
<style>
table {
width:100%;
}
</style>
<title> Class By Class Enrollment Number Listing </title>
</head>
<body>
<?PHP
f_TermLevelStatList();
?>
</body>
</html>
Here is the code for func_highlevel_statlist.php:
<?PHP
function f_TermLevelStatList(){
include_once "./gh_config/db_conn.php";
$sql = "SELECT * FROM barpsc.VW_NumSections_ALL_STATS;";
$Num_Sec_All_Stat = $PDO->query($sql);
?>
<div id="SectStatsAllByTerm">
<h2> Section Stats Summer 2014 to Spring 2023</h2>
<h3> As of Friday, November 18, 2022 </h3>
<table class="TermLevelStats" style="border-collapse:collapse;padding:2pt;">
<thead>
<tr style='border-bottom:2pt solid blue;'>
<th style='padding: 5px;'>Term</th>
<th style='padding: 5px;'>All Sections</th>
<th style='padding: 5px;'>Instructor Sections</th>
<th style='padding: 5px;'>Non-Instr. Sections</th>
<th style='padding: 5px;'>Active</th>
<th style='padding: 5px;'>Cancelled</th>
<th style='padding: 5px;'>Tentative</th>
<th style='padding: 5px;'>Stop Enrl</th>
</tr>
</thead>
<tbody>
<?PHP
//display body of table
while ($row_num_sec_all_stat = $Num_Sec_All_Stat->fetch()){
echo "<tr style='border-bottom:1pt solid blue;'>\n";
echo "<th scope='row' style='text-align:right;'>".$row_num_sec_all_stat['Term']."</th>\n";
echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_ALL']."</td>\n";
echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_INSTR']."</td>\n";
echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_NONINSTR']."</td>\n";
echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_ACTIVE']."</td>\n";
echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_CANCELLED']."</td>\n";
echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_TENTATIVE']."</td>\n";
echo "<td style='text-align:right;'>".$row_num_sec_all_stat['SECTS_STOP_ENRL']."</td>\n";
echo "</tr>\n";
}
?>
</tbody>
</table>
</div>
<?PHP
}
?>