Home  >  Article  >  Backend Development  >  Teach you step by step how to develop a news release website with PHP

Teach you step by step how to develop a news release website with PHP

王林
王林Original
2023-10-27 18:04:441297browse

Teach you step by step how to develop a news release website with PHP

With the popularity and rapid development of the Internet, news release websites have become an important channel for people to obtain news information. As a powerful server-side programming language, PHP is widely used in website development. This article will teach you step by step how to develop a news release website using PHP.

  1. Design database structure
    Before you start writing code, you need to design the structure of the database. A basic news release website mainly contains elements such as news category, news title, news content, release time and so on. You can create a database named news, and create a table named news_articles in it, containing fields such as id, category, title, content, timestamp, etc.
  2. Create database connection
    In PHP, we can use extensions such as mysqli or PDO to connect to the database. Create a file named db_connect.php to store the database connection code and include it in other files that need to connect to the database. The sample code is as follows:
<?php
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'password';
$db_name = 'news';

$conn = new mysqli($db_host, $db_username, $db_password, $db_name);

if ($conn->connect_error) {
    die("数据库连接失败:" . $conn->connect_error);
}
  1. Create a news classification page
    The news classification page is used to display a list of news in different categories. Create a file named category.php in the root directory and write the following code:
<?php
include 'db_connect.php';

$category = $_GET['category'];

$sql = "SELECT * FROM news_articles WHERE category='$category'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "<h3>" . $row['title'] . "</h3>";
        echo "<p>" . $row['content'] . "</p>";
    }
} else {
    echo "暂无新闻";
}

$conn->close();
  1. Create a news details page
    The news details page is used to display the details of a single news content. Create a file named news.php in the root directory and write the following code:
<?php
include 'db_connect.php';

$id = $_GET['id'];

$sql = "SELECT * FROM news_articles WHERE id='$id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    echo "<h1>" . $row['title'] . "</h1>";
    echo "<p>" . $row['content'] . "</p>";
} else {
    echo "新闻不存在";
}

$conn->close();
  1. Create homepage
    The homepage is the homepage of the news release website, used to display the latest news news list. Create a file named index.php in the root directory and write the following code:
<?php
include 'db_connect.php';

$sql = "SELECT * FROM news_articles ORDER BY timestamp DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "<h3>" . $row['title'] . "</h3>";
        echo "<p>" . $row['content'] . "</p>";
    }
} else {
    echo "暂无新闻";
}

$conn->close();
  1. Add news publishing function
    In order to be able to publish news, we need to create a file named admin.php file and write the following code:
<?php
include 'db_connect.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $category = $_POST['category'];
    $title = $_POST['title'];
    $content = $_POST['content'];
    $timestamp = date('Y-m-d H:i:s');

    $sql = "INSERT INTO news_articles (category, title, content, timestamp) VALUES ('$category', '$title', '$content', '$timestamp')";
    if ($conn->query($sql) === TRUE) {
        echo "发布成功";
    } else {
        echo "发布失败:" . $conn->error;
    }
}

$conn->close();
?>

<form method="POST" action="admin.php">
    <input type="text" name="category" placeholder="分类" required>
    <input type="text" name="title" placeholder="标题" required>
    <textarea name="content" placeholder="内容" required></textarea>
    <button type="submit">发布新闻</button>
</form>

At this point, we have completed the development of a simple news release website. You can further optimize and expand according to actual needs, such as adding user authentication, news editing, comments and other functions. I hope this article can help you quickly get started developing a news release website in PHP.

The above is the detailed content of Teach you step by step how to develop a news release website with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn