<?php 
session_start();
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
 
// Include configuration
include('con2.php');

// Ensure POST data is set
$pee = isset($_POST['term']) ? $_POST['term'] : '';
$peea = isset($_POST['session']) ? $_POST['session'] : '';



// Ensure session data is set
$ddd = isset($_SESSION['clay']) ? strtolower($_SESSION['clay']) : '';
$class = isset($_SESSION['class']) ? strtoupper($_SESSION['class']) : '';
$arm = isset($_SESSION['arm']) ? strtoupper($_SESSION['arm']) : '';

// Database table selection logic
$cvb = "$pee$peea";
$asz = "leotrix1_leotrix2";
$dcv = "$cvb$asz";

$bbl = ($pee === "1stterm") ? $asz : $dcv;
$bbl = "leotrix1_leotrix2";
$_SESSION['bbl'] = $bbl;

$sql = "SELECT * FROM `beva1` WHERE password = '$ddd' ORDER BY regno";
$data = $conn->query($sql);

if ($data->num_rows > 0) {
    // Your logic goes here
    // Example: Loop through and perform actions
    while ($row = $data->fetch_assoc()) {
        $svc = $row['regno'];
        // ... continue with logic
    }
} else {
    // Handle no results
    echo "No results found for the given query.";
}

// Database Operations (Insert, Update, Delete, etc.)
function executeQuery($conn, $query) {
    if ($conn->query($query) === TRUE) {
        return true;
    } else {
        echo "Error: " . $conn->error;
        return false;
    }
}

// Example of creating tables and executing queries
$sql = "CREATE TABLE IF NOT EXISTS `#crd$ddd` AS SELECT * FROM `beva1` WHERE password = '$ddd' ORDER BY regno";
executeQuery($conn, $sql);

// Your other queries will follow here...

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>SCHOOL PORTAL</title>
    <style>
        /* Your styling goes here */
        table {
            border-collapse: collapse;
            font: normal 75% arial, sans-serif;
        }
        th.rotate, td.rotate {
            height: 140px;
            white-space: nowrap;
        }
        th.rotate > div, td.rotate > div {
            transform: translate(10px, 51px) rotate(270deg);
            width: 40px;
            height: 40px;
        }
        td, th {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 3px;
        }
        tr:nth-child(even) {
            background-color: #dddddd;
        }
    </style>
</head>
<body>
    <h1>Results Master Sheet</h1>
    <button onclick="window.print();">Print Results</button>
    <table id="datatable">
        <thead>
            <tr>
                <th>Student ID</th>
                <th>Name</th>
                <th>Subject</th>
                <th>Grade</th>
            </tr>
        </thead>
        <tbody>
            <?php
            // Fetch and display results
            $sql = "SELECT * FROM `clean$ddd` ORDER BY POSITION DESC";
            $data = $conn->query($sql);
            while ($row = $data->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . htmlspecialchars($row['regno']) . "</td>";
                echo "<td>" . htmlspecialchars($row['name']) . "</td>";
                echo "<td>" . htmlspecialchars($row['subjects']) . "</td>";
                echo "<td>" . htmlspecialchars($row['grade']) . "</td>";
                echo "</tr>";
            }
            ?>
        </tbody>
    </table>
</body>
</html>

<?php
// Close the database connection
$conn->close();
?>
