Web Technology Lab

Program

8.
a. Develop a PHP program (with HTML/CSS) to keep track of the number of visitors visiting the web page and to display this count of visitors, with relevant headings.

b. Develop a PHP program (with HTML/CSS) to sort the student records which are stored in the database using selection sort





1. Index.php


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Visitor Counter</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin-top: 50px;
    }
    .counter {
      font-size: 24px;
      color: #333;
    }
    .heading {
      font-size: 28px;
      color: #007BFF;
    }
  </style>
</head>
<body>
  <h1 class="heading">Welcome to <b style="color:red;, font-size:35px;" >V</b><b style="color:black;">tudeveloper !</b></h1>

  ?php
    // File to store visitor count
    $file = "visitor_count.txt";

    // Check if file exists, if not create and initialize with 0
    if (!file_exists($file)) {
        file_put_contents($file, 0);
    }

    // Read current visitor count
    $visitorCount = (int)file_get_contents($file);

    // Increment visitor count
    $visitorCount++;

    // Update the file
    file_put_contents($file, $visitorCount);

    echo "<p class='counter'>Total Visitors: $visitorCount</p>";
  ?>
</body>
</html>
    
     
     




Output:
output













Database:- students_db


CREATE DATABASE student_db;
USE student_db;

CREATE TABLE students (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  grade INT NOT NULL
);

INSERT INTO students (name, grade) VALUES
('Abhishek', 85),
('Basava', 92),
('Santosh', 78),
('Aaditya', 88),
('Dileep', 95);

            
    
    




3.student_records.php


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Student Records Sorting</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      text-align: center;
      margin-top: 50px;
    }
    table {
      margin: 20px auto;
      border-collapse: collapse;
      width: 50%;
    }
    th, td {
      border: 1px solid #ccc;
      padding: 10px;
      text-align: center;
    }
    th {
      background-color: #f4f4f4;
    }
  </style>
</head>
<body>
  <h1>Sorted Student Records</h1>

  ?php
  // Database connection
  $conn = new mysqli('localhost', 'root', '', 'student_db');
  
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }

  // Fetch student records
  $result = $conn->query("SELECT * FROM students");
  $students = [];

  while ($row = $result->fetch_assoc()) {
      $students[] = $row;
  }

  // Selection Sort Function
  function selectionSort(&$arr) {
      $n = count($arr);
      for ($i = 0; $i < $n - 1; $i++) {
          $minIdx = $i;
          for ($j = $i + 1; $j < $n; $j++) {
              if ($arr[$j]['grade'] < $arr[$minIdx]['grade']) {
                  $minIdx = $j;
              }
          }
          // Swap
          $temp = $arr[$minIdx];
          $arr[$minIdx] = $arr[$i];
          $arr[$i] = $temp;
      }
  }

  // Sort the records
  selectionSort($students);

  // Display sorted records
  echo "<table>";
  echo "<tr><th>ID</th><th>Name</th><th>Grade</th></tr>";
  foreach ($students as $student) {
      echo "<tr><td>{$student['id']}</td><td>{$student['name']}</td><td>{$student['grade']}</td></tr>";
  }
  echo "</table>";

  $conn->close();
  ?>
</body>
</html>

            
    
    


Output:
output