File size: 7,473 Bytes
102fe5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// Include database connection and authentication
require_once 'includes/db_connect.php';
require_once 'includes/auth_check.php';

// This page is for faculty only
requireFaculty();

// Initialize variables
$students = [];
$searchTerm = '';
$error = '';

// Check if students_info table exists
$tableCheckQuery = "SHOW TABLES LIKE 'students_info'";
$tableExists = $conn->query($tableCheckQuery)->num_rows > 0;

if ($tableExists) {
    // Get student data structure
    $columnsQuery = "SHOW COLUMNS FROM students_info";
    $columnsResult = $conn->query($columnsQuery);
    $studentIdField = null;
    $studentNameFields = [];
    
    // Find student ID and name fields
    while ($column = $columnsResult->fetch_assoc()) {
        if (preg_match('/(student|stud|roll)[\s_-]?(id|number|no)/i', $column['Field'])) {
            $studentIdField = $column['Field'];
        }
        
        if (preg_match('/(student|stud)[\s_-]?(name)/i', $column['Field']) ||
            preg_match('/(first|last|full)[\s_-]?(name)/i', $column['Field'])) {
            $studentNameFields[] = $column['Field'];
        }
    }
    
    // Handle search
    if (isset($_GET['search']) && !empty($_GET['search'])) {
        $searchTerm = trim($_GET['search']);
        
        // Prepare search conditions
        $conditions = [];
        $params = [];
        $types = '';
        
        // Search by ID
        if ($studentIdField) {
            $conditions[] = "$studentIdField LIKE ?";
            $params[] = "%$searchTerm%";
            $types .= 's';
        }
        
        // Search by name fields
        foreach ($studentNameFields as $field) {
            $conditions[] = "$field LIKE ?";
            $params[] = "%$searchTerm%";
            $types .= 's';
        }
        
        if (!empty($conditions)) {
            $sql = "SELECT * FROM students_info WHERE " . implode(' OR ', $conditions);
            $stmt = $conn->prepare($sql);
            
            if (!empty($params)) {
                $stmt->bind_param($types, ...$params);
            }
            
            $stmt->execute();
            $result = $stmt->get_result();
            
            if ($result) {
                while ($row = $result->fetch_assoc()) {
                    $students[] = $row;
                }
            } else {
                $error = "Error executing search query: " . $conn->error;
            }
        } else {
            $error = "No suitable fields found for searching.";
        }
    } else {
        // Get all students if no search
        $sql = "SELECT * FROM students_info";
        $result = $conn->query($sql);
        
        if ($result) {
            while ($row = $result->fetch_assoc()) {
                $students[] = $row;
            }
        } else {
            $error = "Error fetching students: " . $conn->error;
        }
    }
} else {
    $error = "Student information table not found in the database.";
}

// Include header
include 'includes/header.php';
?>

<div class="row mb-4">

    <div class="col-md-12">

        <nav aria-label="breadcrumb">

            <ol class="breadcrumb">

                <li class="breadcrumb-item"><a href="index.php">Home</a></li>

                <li class="breadcrumb-item active">Students</li>

            </ol>

        </nav>

        <div class="d-flex justify-content-between align-items-center">

            <h2><i class="fas fa-user-graduate me-2"></i> Students Information</h2>

        </div>

        <p class="lead">View and search for student details</p>

    </div>

</div>



<div class="row mb-4">

    <div class="col-md-12">

        <div class="card shadow-sm">

            <div class="card-body">

                <form method="GET" action="students.php" class="row g-3">

                    <div class="col-md-10">

                        <div class="input-group">

                            <span class="input-group-text"><i class="fas fa-search"></i></span>

                            <input type="text" name="search" class="form-control" placeholder="Search by ID or name" value="<?php echo htmlspecialchars($searchTerm); ?>">

                        </div>

                    </div>

                    <div class="col-md-2">

                        <button type="submit" class="btn btn-primary w-100">Search</button>

                    </div>

                </form>

            </div>

        </div>

    </div>

</div>



<?php if (!empty($error)): ?>

    <div class="alert alert-danger">

        <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?>

    </div>

<?php endif; ?>



<div class="row">

    <div class="col-md-12">

        <div class="card shadow-sm">

            <div class="card-header bg-primary text-white">

                <h5 class="mb-0"><i class="fas fa-list me-2"></i> Students List</h5>

            </div>

            <div class="card-body">

                <?php if (count($students) > 0): ?>

                    <div class="table-responsive">

                        <table class="table table-striped table-hover">

                            <thead class="table-light">

                                <tr>

                                    <?php 

                                    // Get the first student to determine columns

                                    $firstStudent = $students[0];

                                    foreach ($firstStudent as $key => $value): 

                                    ?>

                                        <th><?php echo htmlspecialchars($key); ?></th>

                                    <?php endforeach; ?>

                                    <th>Actions</th>

                                </tr>

                            </thead>

                            <tbody>

                                <?php foreach ($students as $student): ?>

                                    <tr>

                                        <?php foreach ($student as $key => $value): ?>

                                            <td><?php echo htmlspecialchars($value); ?></td>

                                        <?php endforeach; ?>

                                        <td>

                                            <?php if (isset($studentIdField) && isset($student[$studentIdField])): ?>

                                                <a href="student_projects.php?id=<?php echo urlencode($student[$studentIdField]); ?>" class="btn btn-sm btn-info">

                                                    <i class="fas fa-project-diagram"></i> View Projects

                                                </a>

                                            <?php endif; ?>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>

                            </tbody>

                        </table>

                    </div>

                <?php else: ?>

                    <div class="alert alert-info">

                        <i class="fas fa-info-circle me-2"></i> No students found.

                    </div>

                <?php endif; ?>

            </div>

        </div>

    </div>

</div>



<?php

// Include footer

include 'includes/footer.php';



// Close connection

$conn->close();

?>