<?php
// Fetch the marksheet page
$url = 'http://localhost/bidda/reports/all_subjects_marksheet.php?student_id=6279&session=2025&exam_type=final';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$html = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP Code: $httpCode\n\n";

if ($httpCode == 200) {
    // Check if tbody exists and has content
    if (strpos($html, '<tbody>') !== false) {
        echo "Found tbody tag\n";

        // Extract tbody content
        $start = strpos($html, '<tbody>');
        $end = strpos($html, '</tbody>', $start);
        $tbodyContent = substr($html, $start, $end - $start + 8);

        echo "Tbody content length: " . strlen($tbodyContent) . "\n";

        // Count table rows
        $rowCount = substr_count($tbodyContent, '<tr');
        echo "Number of table rows: $rowCount\n\n";

        // Show first few rows
        if ($rowCount > 0) {
            $rows = explode('<tr', $tbodyContent);
            for ($i = 1; $i < min(4, count($rows)); $i++) {
                echo "Row $i: " . substr($rows[$i], 0, 100) . "...\n";
            }
        } else {
            echo "No table rows found in tbody!\n";
        }
    } else {
        echo "No tbody tag found!\n";
    }

    // Check if there are any subjects mentioned
    $subjectCount = substr_count($html, 'subject_name');
    echo "\nSubject references in HTML: $subjectCount\n";

    // Check for specific subjects
    $subjects = ['Bangla', 'English', 'Math', 'Science'];
    foreach ($subjects as $subject) {
        $count = substr_count($html, $subject);
        echo "Occurrences of '$subject': $count\n";
    }

} else {
    echo "Failed to fetch page\n";
    echo "Response: " . substr($html, 0, 500) . "\n";
}
?>

