Create news page on website with php

Here is a basic example of how you can create a news page on a website using PHP:

Step 1: Create a database and table

First, you need to create a database and a table to store your news articles. You can use a tool like phpMyAdmin to create a database and a table with the following structure:

CREATE TABLE news (
  id INT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(255),
  content TEXT,
  date DATE,
  author VARCHAR(50)
);

Step 2: Create a PHP script to retrieve news articles

Create a new PHP file (e.g. news.php) and add the following code:

<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database_name");

// Check connection
if (!$conn) {
  die("Connection failed: ". mysqli_connect_error());
}

// Retrieve news articles
$query = "SELECT * FROM news ORDER BY date DESC";
$result = mysqli_query($conn, $query);

// Check if there are any results
if (mysqli_num_rows($result) > 0) {
  while ($row = mysqli_fetch_assoc($result)) {
    echo "<h2>". $row['title']. "</h2>";
    echo "<p>". $row['content']. "</p>";
    echo "<p>By ". $row['author']. " on ". $row['date']. "</p>";
    echo "<hr>";
  }
} else {
  echo "No news articles found.";
}

// Close the database connection
mysqli_close($conn);
?>

This script connects to the database, retrieves all news articles in descending order of date, and then loops through the results to display each article.

Step 3: Create a news page on your website

Create a new HTML file (e.g. news.html) and add the following code:

<!DOCTYPE html>
<html>
<head>
  <title>News Page</title>
</head>
<body>
  <h1>News Page</h1>
  <?php include 'news.php';?>
</body>
</html>

This file includes the news.php script and displays the news articles on the page.

Step 4: Add a form to submit new news articles

Create a new HTML file (e.g. submit_news.html) and add the following code:

<!DOCTYPE html>
<html>
<head>
  <title>Submit News Article</title>
</head>
<body>
  <h1>Submit News Article</h1>
  <form action="submit_news.php" method="post">
    <label for="title">Title:</label>
    <input type="text" id="title" name="title"><br><br>
    <label for="content">Content:</label>
    <textarea id="content" name="content"></textarea><br><br>
    <label for="author">Author:</label>
    <input type="text" id="author" name="author"><br><br>
    <input type="submit" value="Submit">
  </form>
</body>
</html>

This file creates a form with fields for title, content, and author. When the form is submitted, the data will be sent to the submit_news.php script.

Step 5: Create a PHP script to handle form submissions

Create a new PHP file (e.g. submit_news.php) and add the following code:

<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database_name");

// Check connection
if (!$conn) {
  die("Connection failed: ". mysqli_connect_error());
}

// Retrieve form data
$title = $_POST['title'];
$content = $_POST['content'];
$author = $_POST['author'];

// Insert new news article into the database
$query = "INSERT INTO news (title, content, author, date) VALUES ('$title', '$content', '$author', NOW())";
mysqli_query($conn, $query);

// Close the database connection
mysqli_close($conn);

// Redirect to the news page
header("Location: news.php");
?>

This script connects to the database, retrieves the form data, and inserts a new news article into the database. It then redirects the user to the news page.

That's it! You now have a basic news page on your website that allows users to submit new news articles. You can customize the design and functionality of the page as needed.