Home  >  Article  >  Backend Development  >  How to use PHP to implement the advertising management function of CMS system

How to use PHP to implement the advertising management function of CMS system

王林
王林Original
2023-08-07 12:33:161276browse

How to use PHP to implement the advertising management function of CMS system

With the rapid development of the Internet, websites have become one of the important channels for promotion in various industries. In websites, advertising is a common way to make money. Therefore, it is very important for a CMS system to implement a complete advertising management function. This article will introduce how to use PHP to implement the advertising management function of the CMS system and provide relevant code examples.

1. Create an advertising table

First, we need to create an advertising table in the database to store advertising-related information. An advertisement table containing the necessary fields can be created using the following SQL statement:

CREATE TABLE `advertisements` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `img_url` varchar(255) NOT NULL,
  `link_url` varchar(255) NOT NULL,
  `start_time` datetime NOT NULL,
  `end_time` datetime NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

In the advertisement table, we have included the following fields:

  • id: the unique identifier of the advertisement, use Self-increasing integer
  • title: The title of the advertisement
  • img_url: The URL address of the advertisement image
  • link_url: The URL address that jumps after clicking on the advertisement
  • start_time: The start time of the advertisement
  • end_time: The end time of the advertisement
  • status: The status of the advertisement, 0 means disabled, 1 means enabled

2. Add Advertising

Next, we need to add a page to the CMS system to add advertising information. You can use the following code example to create an add advertising page:

<?php
  if($_POST) {
    $title = $_POST['title'];
    $img_url = $_POST['img_url'];
    $link_url = $_POST['link_url'];
    $start_time = $_POST['start_time'];
    $end_time = $_POST['end_time'];
    $status = $_POST['status'];

    // TODO: 插入广告信息到数据库中
    // $insertQuery = "INSERT INTO `advertisements` (title, img_url, link_url, start_time, end_time, status) VALUES ('$title', '$img_url', '$link_url', '$start_time', '$end_time', '$status')";
    // $insertResult = mysqli_query($connection, $insertQuery);
    
    if($insertResult) {
      echo '广告添加成功!';
    } else {
      echo '广告添加失败,请重试!';
    }
  }
?>

<form method="post" action="">
  <label for="title">标题:</label>
  <input type="text" name="title" /><br>
  <label for="img_url">图片链接:</label>
  <input type="text" name="img_url" /><br>
  <label for="link_url">跳转链接:</label>
  <input type="text" name="link_url" /><br>
  <label for="start_time">开始时间:</label>
  <input type="text" name="start_time" /><br>
  <label for="end_time">结束时间:</label>
  <input type="text" name="end_time" /><br>
  <label for="status">状态:</label>
  <input type="text" name="status" /><br>
  <input type="submit" value="添加" />
</form>

In the code example, we obtain the advertising information submitted by the user through the POST method, and then insert it into the advertising table in the database.

3. Manage Advertisements

In addition to adding advertising functions, we also need to implement advertising management functions, including editing, deleting, enabling/disabling, etc. You can use the following code sample to create an advertising management page:

<?php
  // TODO: 查询数据库中的广告列表
  // $query = "SELECT * FROM `advertisements`";
  // $result = mysqli_query($connection, $query);

  while($row = mysqli_fetch_assoc($result)) {
    echo '<div>';
    echo '<h4>'.$row['title'].'</h4>';
    echo '<img src="'.$row['img_url'].'" />';
    echo '<a href="'.$row['link_url'].'">点击跳转</a>';
    echo '<p>开始时间:'.$row['start_time'].'</p>';
    echo '<p>结束时间:'.$row['end_time'].'</p>';
    echo '<p>状态:'.$row['status'].'</p>';
    echo '<a href="edit.php?id='.$row['id'].'">编辑</a> ';
    echo '<a href="delete.php?id='.$row['id'].'">删除</a>';
    echo '</div>';
  }
?>

In the code sample, we query the list of ads in the database and display them in the list through a loop. At the same time, edit and delete links are provided for each advertisement to facilitate administrators to manage advertisements.

4. Edit ads

Editing ads is an important part of the advertising management function. We can use the following code example to create a page for editing ads:

<?php
  // TODO: 获取指定ID的广告信息
  // $id = $_GET['id'];
  // $query = "SELECT * FROM `advertisements` WHERE id = $id";
  // $result = mysqli_query($connection, $query);
  // $ad = mysqli_fetch_assoc($result);

  if($_POST) {
    $title = $_POST['title'];
    $img_url = $_POST['img_url'];
    $link_url = $_POST['link_url'];
    $start_time = $_POST['start_time'];
    $end_time = $_POST['end_time'];
    $status = $_POST['status'];

    // TODO: 更新广告信息
    // $updateQuery = "UPDATE `advertisements` SET title = '$title', img_url = '$img_url', link_url = '$link_url', start_time = '$start_time', end_time = '$end_time', status = '$status' WHERE id = $id";
    // $updateResult = mysqli_query($connection, $updateQuery);
    
    if($updateResult) {
      echo '广告编辑成功!';
    } else {
      echo '广告编辑失败,请重试!';
    }
  }
?>

<form method="post" action="">
  <label for="title">标题:</label>
  <input type="text" name="title" value="<?php echo $ad['title']; ?>" /><br>
  <label for="img_url">图片链接:</label>
  <input type="text" name="img_url" value="<?php echo $ad['img_url']; ?>" /><br>
  <label for="link_url">跳转链接:</label>
  <input type="text" name="link_url" value="<?php echo $ad['link_url']; ?>" /><br>
  <label for="start_time">开始时间:</label>
  <input type="text" name="start_time" value="<?php echo $ad['start_time']; ?>" /><br>
  <label for="end_time">结束时间:</label>
  <input type="text" name="end_time" value="<?php echo $ad['end_time']; ?>" /><br>
  <label for="status">状态:</label>
  <input type="text" name="status" value="<?php echo $ad['status']; ?>" /><br>
  <input type="submit" value="保存" />
</form>

In the page for editing ads, we obtain the advertising information of the specified ID and display it in the form. Users can modify advertising information and submit it to the database for update.

Summary

Through the above sample code, the advertising management function is implemented in the CMS system. With the cooperation of PHP and database, we can add, edit, delete advertisements, etc., providing convenience in advertisement management. Of course, in actual development, we also need to carry out appropriate functional expansion according to needs, such as permission control, search functions, etc., to provide a better user experience.

The above is the detailed content of How to use PHP to implement the advertising management function of CMS system. 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