WP PROGRAM AND OUTPUT 1
WP PROGRAM AND OUTPUT 1
<!DOCTYPE html>
<html>
<head>
<title>Color Change by Day</title>
<?php
// Array of days with corresponding colors
$daysOfWeek = array(
'Sunday' => '#FF5733',
'Monday' => '#33FF57',
'Tuesday' => '#3357FF',
'Wednesday' => '#FFFF33',
'Thursday' => '#FF33FF',
'Friday' => '#33FFFF',
'Saturday' => '#FF3333'
);
OUTPUT:
PROGRAM: 9
Write a simple program in PHP for i) generating Prime
number ii) generate Fibonacci series.
o<?php
function generatePrimes($maxNumber) {
echo "Prime numbers up to $maxNumber: ";
for ($number = 2; $number <= $maxNumber; $number++) {
$isPrime = true;
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i == 0) {
$isPrime = false;
break;
}
}
if ($isPrime) {
echo $number . " ";
}
}
echo "<br>";
}
function fibonacciSeries($numTerms) {
$first = 0;
$second = 1;
echo "Fibonacci Series ($numTerms terms): ";
for ($i = 0; $i < $numTerms; $i++) {
echo $first . " ";
$next = $first + $second;
$first = $second;
$second = $next;
}
echo "<br>";
}
PROGRAM :10
Write a PHP program to remove duplicates from a sorted
list
<?php
function removeDuplicates($array) {
$result = array_values(array_unique($array));
return $result;
}
PROGRAM 12:
Write a simple program in PHP for Searching of data by
different criteria
<?php
$data = [
['id' => 1, 'name' => 'alan', 'age' => 30],
['id' => 2, 'name' => 'sebin', 'age' => 35],
['id' => 3, 'name' => 'sebin', 'age' => 50],
['id' => 4, 'name' => 'sanu', 'age' => 45],
['id' => 5, 'name' => 'ghh', 'age' => 50] // Changed id to 5 for Saatvik
];
return $results;
}
$criteria = ['age' => 50];
OUTPUT:
PROGRAM :13
Write a function in PHP to generate captcha code
<?php
session_start();
function generateCaptchaCode($length = 6) {
$characters =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
return $randomString;
}
?>
OUTPUT:
PROGRAM 14:
<?php
// Database connection
$conn = new mysqli("localhost", "root", "", "myDB"); // Update with your
credentials
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Display Image by ID
if (isset($_GET['id'])) {
$id = intval($_GET['id']);
$stmt = $conn->prepare("SELECT image_type, image_data FROM images WHERE id
= ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($imageType, $imageData);
if ($stmt->fetch()) {
header("Content-Type: " . $imageType);
echo $imageData;
}
$stmt->close();
exit;
}
if (!in_array($imageType, $allowedTypes)) {
die("Invalid file type. Only JPEG, PNG, and GIF are allowed.");
}
$imageData = file_get_contents($imageTmpName);
<!DOCTYPE html>
<html>
<head>
<title>Image Upload and Display</title>
</head>
<body>
<h1>Upload and Display Images</h1>
<!-- Image Upload Form -->
<form method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="image" id="image" required>
<input type="submit" value="Upload Image" name="submit">
</form>
<h2>Uploaded Images:</h2>
<?php displayImages($conn); ?>
OUTPUT:
PROGRAM 15:
<!DOCTYPE html>
<html>
<head>
<title>File Reader and Writer</title>
</head>
<body>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
?>
OUTPUT:
PROGRAM 16:
<!DOCTYPE html>
<html>
<head>
<title>Student Database Operations</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "studentdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Add Action
if ($action == "Add") {
$add_sql = "INSERT INTO students (regno, name, age, course) VALUES
('$regno', '$name', '$age', '$course')";
if ($conn->query($add_sql) === TRUE) {
echo "New record added successfully.<br><br>";
} else {
echo "Error adding record: " . $conn->error . "<br>";
}
}
// Update Action
elseif ($action == "Update") {
$update_sql = "UPDATE students SET name = '$name', age = '$age',
course = '$course' WHERE regno = '$regno'";
if ($conn->query($update_sql) === TRUE) {
echo "Record updated successfully.<br><br>";
} else {
echo "Error updating record: " . $conn->error . "<br>";
}
}
// Delete Action
elseif ($action == "Delete") {
$delete_sql = "DELETE FROM students WHERE regno = '$regno'";
if ($conn->query($delete_sql) === TRUE) {
echo "Record deleted successfully.<br><br>";
} else {
echo "Error deleting record: " . $conn->error . "<br>";
}
}
}
// Close connection
$conn->close();
?>
</body>
</html>
OUTPUT:
PROGRAM 17:
<!DOCTYPE html>
<html>
<head>
<title>Input Validation</title>
</head>
<body>
<form method="post">
Name: <input type="text" name="name"><br><br>
Email: <input type="text" name="email"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
// Validate Name
if (empty($name)) {
echo "Name is required.<br><br>";
} else {
echo "Name: " . $name . "<br><br>";
}
// Validate Email
if (empty($email)) {
echo "Email is required.<br><br>";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format.<br><br>";
} else {
echo "Email: " . $email . "<br><br>";
}
}
?>
</body>
</html>
OUTPUT:
PROGRAM 18:
<!DOCTYPE html>
<html>
<head>
<title>Cookie Handler</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Set the cookie with a name and value from the form
setcookie("username", $_POST['username'], time() + (10 * 30), "/");
echo "<p>Cookie set. Reload the page to see the cookie value.</p>";
}
OUTPUT:
PROGRAM 19:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Skyward Institute of Computer Applications</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
line-height: 1.6;
}
header {
background-color: #f8f9fa;
padding: 20px;
text-align: center;
border-bottom: 1px solid #ddd;
}
nav {
margin: 20px 0;
text-align: center;
}
nav a {
margin: 0 15px;
text-decoration: none;
color: #007bff;
font-weight: bold;
}
nav a:hover {
text-decoration: underline;
}
main {
padding: 20px;
text-align: center;
}
main h2 {
color: #333;
}
</style>
</head>
<body>
<header>
<img src="logo.jpg" alt="Skyward Institute of Computer Applications
Logo" width="100">
<h1>Skyward Institute of Computer Applications</h1>
<p>157, 3RD MAIN, CHAMRAJPET BANGALORE 560018</p>
</header>
<nav>
<a href="index.php">Home</a>
<a href="about.php">About Us</a>
<a href="courses.php">Courses</a>
<a href="contact.php">Contact Us</a>
</nav>
<main>
<h2>Welcome to Skyward Institute of Computer Applications</h2>
<p>We provide the best training in computer application courses.</p>
</main>
</body>
</html>
OUTPUT:
PROGRAM 20:
<?php
try {
echo divide(10, 2) . "<br>"; // Should work
echo divide(10, 0) . "<br>"; // Should throw exception
} catch (Exception $e) {
echo "Division error: ". $e->getMessage() . "<br>";
}
try {
CheckDateFormat("2023-03-10"); // Should work and confirm validity
CheckDateFormat("10/03/2023"); // Should throw exception
} catch (Exception $e) {
echo "Date error: " . $e->getMessage() . "<br>";
}
?>
OUTPUT: