Online breaking news web application in php
Here is a basic outline of a online breaking news web application in PHP:
Database Design
- Create a database named
breaking_news
with the following tables:news
:id
(primary key, auto-increment)title
description
category
(e.g. politics, sports, entertainment)date_published
author
categories
:id
(primary key, auto-increment)name
comments
:id
(primary key, auto-increment)news_id
(foreign key referencing thenews
table)comment_text
author
date_commented
PHP Code
index.php
: This will be the main page that displays the latest news.<?php // Connect to the database $conn = mysqli_connect("localhost", "username", "password", "breaking_news");
// Check connection if (!$conn) { die("Connection failed: ". mysqli_connect_error()); }
// Get the latest news $query = "SELECT * FROM news ORDER BY date_published DESC LIMIT 10"; $result = mysqli_query($conn, $query);
// Display the news while ($row = mysqli_fetch_assoc($result)) { echo "
". $row['title']. "
"; echo "". $row['description']. "
"; echo "Category: ". $row['category']. "
"; echo "Published: ". $row['date_published']. "
"; echo "Author: ". $row['author']. "
"; echo ""; }
// Close the connection mysqli_close($conn); ?>
* `add_news.php`: This will be the page where users can submit new news articles.
```php
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "breaking_news");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Get the form data
$title = $_POST['title'];
$description = $_POST['description'];
$category = $_POST['category'];
$author = $_POST['author'];
// Insert the new news article into the database
$query = "INSERT INTO news (title, description, category, author) VALUES ('$title', '$description', '$category', '$author')";
mysqli_query($conn, $query);
// Close the connection
mysqli_close($conn);
// Redirect to the main page
header("Location: index.php");
?>
view_news.php
: This will be the page where users can view a specific news article.<?php // Connect to the database $conn = mysqli_connect("localhost", "username", "password", "breaking_news");
// Check connection if (!$conn) { die("Connection failed: ". mysqli_connect_error()); }
// Get the news ID from the URL $news_id = $_GET['id'];
// Get the news article from the database $query = "SELECT * FROM news WHERE id = '$news_id'"; $result = mysqli_query($conn, $query);
// Display the news article while ($row = mysqli_fetch_assoc($result)) { echo "
". $row['title']. "
"; echo "". $row['description']. "
"; echo "Category: ". $row['category']. "
"; echo "Published: ". $row['date_published']. "
"; echo "Author: ". $row['author']. "
"; echo ""; }
// Get the comments for the news article $query = "SELECT * FROM comments WHERE news_id = '$news_id'"; $result = mysqli_query($conn, $query);
// Display the comments while ($row = mysqli_fetch_assoc($result)) { echo "
". $row['comment_text']. "
"; echo "Author: ". $row['author']. "
"; echo "Commented: ". $row['date_commented']. "
"; echo ""; }
// Close the connection mysqli_close($conn); ?>
* `add_comment.php`: This will be the page where users can submit new comments for a news article.
```php
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "breaking_news");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Get the form data
$news_id = $_POST['news_id'];
$comment_text = $_POST['comment_text'];
$author = $_POST['author'];
// Insert the new comment into the database
$query = "INSERT INTO comments (news_id, comment_text, author) VALUES ('$news_id', '$comment_text', '$author')";
mysqli_query($conn, $query);
// Close the connection
mysqli_close($conn);
// Redirect to the view news page
header("Location: view_news.php?id=$news_id");
?>
Front-end Code
index.html
: This will be the main page that displays the latest news.<!DOCTYPE html> <html> <head> <title>Breaking News</title> </head> <body> <h1>Breaking News</h1> <ul> <?php include 'index.php';?> </ul> </body> </html>
view_news.html
: This will be the page where users can view a specific news article.<!DOCTYPE html> <html> <head> <title>Breaking News</title> </head> <body> <h1><?php echo $row['title'];?></h1> <p><?php echo $row['description'];?></p> <p>Category: <?php echo $row['category'];?></p> <p>Published: <?php echo $row['date_published'];?></p> <p>Author: <?php echo $row['author'];?></p> <hr> <h2>Comments</h2> <ul> <?php while ($row = mysqli_fetch_assoc($result)) {?> <li> <p><?php echo $row['comment_text'];?></p> <p>Author: <?php echo $row['author'];?></p> <p>Commented: <?php echo $row['date_commented'];?></p> </li> <?php }?> </ul> </body> </html>
This is a basic outline of a online breaking news web application in PHP. You will need to modify the code to fit your specific needs and add additional features such as user authentication, news categorization, and search functionality.