ABC
ABC
ATTOCK CAMPUS
ASSIGNMENT: 03
SUBMITTED BY:
RIDDA ZAINAB (SP22-BCS-039)
SUBMITTED TO:
SIR BABAR SHAHZAD
COURSE NAME:
WEB TECHNOLOGY
DATE:
29-NOV-2024
QUESTION NO 1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess Board</title>
<style>
table {
width: 400px;
border-collapse: collapse; }
td {
width: 30px;
height: 30px; }
.black {
background-color: black; }
.white {
background-color: white; }
</style>
</head>
<body>
<table border="1">
<?php
for ($row = 0; $row < 8; $row++) {
echo "<tr>";
for ($col = 0; $col < 8; $col++) {
$color = ($row + $col) % 2 == 0 ? "white" : "black";
echo "<td class='$color'></td>";
}
echo "</tr>"; } ?>
</table>
</body>
</html>
QUESTION NO 2:
Index.php
<?php
$articles = file_exists('articles.json') ? json_decode(file_get_contents('articles.json'),
true) : [];
if (isset($_GET['delete'])) {
$id = intval($_GET['delete']);
if ($id >= 0 && $id < count($articles)) {
array_splice($articles, $id, 1);
file_put_contents('articles.json', json_encode($articles));
}
header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TechBlog - Homepage</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f9f9f9;
margin: 0;
padding: 20px;
}
.container {
max-width: 800px;
margin: auto;
background: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1, h2 {
text-align: center;
color: #333;
}
.btn {
display: inline-block;
margin: 20px auto;
padding: 10px 20px;
background-color: #007BFF;
color: white;
text-decoration: none;
border-radius: 5px;
text-align: center;
}
.btn:hover {
background-color: #0056b3;
}
.center-btn {
text-align: center;
}
.delete-btn {
background-color: #dc3545;
color: white;
border: none;
padding: 5px 10px;
border-radius: 5px;
cursor: pointer;
}
.delete-btn:hover {
background-color: #c82333;
}
ul {
list-style: none;
padding: 0;
}
li {
margin: 10px 0;
background: #f1f1f1;
padding: 10px;
border-radius: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
a {
text-decoration: none;
color: #333;
font-weight: bold;
}
a:hover {
color: #007BFF;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to TechBlog</h1>
<div class="center-btn">
<a href="create_article.php" class="btn">Create a New Article</a>
</div>
<h2>Articles</h2>
<ul>
<?php if (!empty($articles)): ?>
<?php foreach ($articles as $index => $article): ?>
<li>
<a href="view_article.php?id=<?= $index ?>">
<?= htmlspecialchars($article['title']) ?> by <?=
htmlspecialchars($article['author']) ?>
</a>
<form method="get" style="margin: 0;">
<input type="hidden" name="delete" value="<?= $index ?>">
<button type="submit" class="delete-btn">Delete</button>
</form>
</li>
<?php endforeach; ?>
<?php else: ?>
<p>No articles found. Start by creating one!</p>
<?php endif; ?>
</ul>
</div>
</body>
</html>
Create_article.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$title = htmlspecialchars($_POST['title']);
$content = htmlspecialchars($_POST['content']);
$author = htmlspecialchars($_POST['author']);
$articles = file_exists('articles.json') ? json_decode(file_get_contents('articles.json'),
true) : [];
$articles[] = ['title' => $title, 'content' => $content, 'author' => $author];
file_put_contents('articles.json', json_encode($articles));
header("Location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create Article</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f9f9f9;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: auto;
background: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
text-align: center;
color: #333;
}
form {
margin-top: 20px;
}
input, textarea {
width: 580px;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 5px;
}
.btn {
width: 100%;
padding: 10px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
text-align: center;
}
.btn:hover {
background-color: #218838;
}
</style>
</head>
<body>
<div class="container">
<h1>Create a New Article</h1>
<form method="post" action="create_article.php">
<input type="text" name="title" placeholder="Enter Article Title" required>
<textarea name="content" rows="5" placeholder="Write Article Content"
required></textarea>
<input type="text" name="author" placeholder="Author Name" required>
<button type="submit" class="btn">Submit</button>
</form>
</div>
</body>
</html>
View_Article.php:
<?php
$articles = file_exists('articles.json') ? json_decode(file_get_contents('articles.json'),
true) : [];
$id = isset($_GET['id']) ? intval($_GET['id']) : -1;
if ($id < 0 || $id >= count($articles)) {
die("Article not found.");
}
$article = $articles[$id];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($article['title']) ?></title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f9f9f9;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: auto;
background: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}
h1 {
text-align: center;
color: #333;
}
.back {
margin-top: 20px;
text-align: center;
}
.btn {
padding: 10px 20px;
background-color: #007BFF;
color: white;
text-decoration: none;
border-radius: 5px;
}
.btn:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1><?= htmlspecialchars($article['title']) ?></h1>
<p><strong>Author:</strong> <?= htmlspecialchars($article['author']) ?></p>
<p><strong>Content: </strong><?= nl2br(htmlspecialchars($article['content'])) ?></p>
<div class="back">
<a href="index.php" class="btn">Back to Homepage</a>
</div>
</div>
</body>
</html>
Home Page:
Viewing an Article: