代码生成(用html+css+mysql+php+js写星际争霸2赛事网站的赛事安排面板)
时间: 2023-09-27 14:04:04 浏览: 180
很高兴为您提供代码生成方案。以下是一个简单的 HTML、CSS、MySQL、PHP 和 JavaScript 代码示例,用于创建一个星际争霸2赛事网站的赛事安排面板。
首先,我们需要创建 MySQL 数据库和表。您可以使用以下 SQL 代码:
```sql
CREATE DATABASE starcraft;
USE starcraft;
CREATE TABLE events (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
date DATETIME NOT NULL,
location VARCHAR(255) NOT NULL,
description TEXT
);
```
接下来,我们将创建一个 HTML 文件,该文件将包含表单以添加新的赛事,并显示现有的赛事安排。以下是 HTML 代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>Starcraft Events</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div class="container">
<h1>Starcraft Events</h1>
<form id="event-form">
<label for="title">Event Title:</label>
<input type="text" id="title" name="title" required>
<label for="date">Date and Time:</label>
<input type="datetime-local" id="date" name="date" required>
<label for="location">Location:</label>
<input type="text" id="location" name="location" required>
<label for="description">Description:</label>
<textarea id="description" name="description"></textarea>
<input type="submit" value="Add Event">
</form>
<table id="event-table">
<thead>
<tr>
<th>Event Title</th>
<th>Date and Time</th>
<th>Location</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<!-- Events will be displayed here dynamically -->
</tbody>
</table>
</div>
</body>
</html>
```
现在,我们将创建一个 CSS 文件,该文件将定义基本的样式和布局。以下是 CSS 代码示例:
```css
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
margin-bottom: 20px;
}
form {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin-bottom: 20px;
}
label {
width: 100%;
margin-bottom: 10px;
}
input[type="text"],
input[type="datetime-local"],
textarea {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #fff;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
resize: vertical;
}
input[type="submit"] {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
th {
background-color: #007bff;
color: #fff;
}
tbody tr:hover {
background-color: #f2f2f2;
}
```
然后,我们将创建一个 PHP 文件,该文件将处理表单提交并将赛事添加到数据库中。以下是 PHP 代码示例:
```php
<?php
// Connect to database
$mysqli = new mysqli("localhost", "username", "password", "starcraft");
// Check connection
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
}
// Check if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get values from form
$title = $_POST["title"];
$date = date("Y-m-d H:i:s", strtotime($_POST["date"]));
$location = $_POST["location"];
$description = $_POST["description"];
// Prepare SQL statement
$stmt = $mysqli->prepare("INSERT INTO events (title, date, location, description) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $title, $date, $location, $description);
// Execute statement
if ($stmt->execute()) {
echo "Event added successfully!";
} else {
echo "Error adding event: " . $stmt->error;
}
// Close statement
$stmt->close();
}
// Get all events from database
$result = $mysqli->query("SELECT * FROM events ORDER BY date DESC");
// Generate HTML for events table
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["title"] . "</td>";
echo "<td>" . date("F j, Y g:i a", strtotime($row["date"])) . "</td>";
echo "<td>" . $row["location"] . "</td>";
echo "<td>" . $row["description"] . "</td>";
echo "</tr>";
}
// Close database connection
$mysqli->close();
?>
```
最后,我们将创建一个 JavaScript 文件,该文件将使用 AJAX 从服务器获取赛事数据,并将其添加到表格中。以下是 JavaScript 代码示例:
```javascript
// Get event table element
var eventTable = document.getElementById("event-table");
// Function to add event to table
function addEventToTable(event) {
// Create new table row
var newRow = eventTable.insertRow(-1);
// Add event data to row
var titleCell = newRow.insertCell(0);
titleCell.innerHTML = event.title;
var dateCell = newRow.insertCell(1);
dateCell.innerHTML = event.date;
var locationCell = newRow.insertCell(2);
locationCell.innerHTML = event.location;
var descriptionCell = newRow.insertCell(3);
descriptionCell.innerHTML = event.description;
}
// Function to handle form submit
function handleFormSubmit(event) {
// Prevent default form submission
event.preventDefault();
// Get form data
var formData = new FormData(event.target);
// Send form data to server
var xhr = new XMLHttpRequest();
xhr.open("POST", "add_event.php", true);
xhr.onload = function() {
if (xhr.status == 200) {
alert(xhr.responseText);
// Clear form fields
event.target.reset();
// Reload events table
loadEvents();
}
};
xhr.send(formData);
}
// Function to load events from server
function loadEvents() {
// Clear existing table rows
eventTable.getElementsByTagName("tbody")[0].innerHTML = "";
// Send AJAX request to server
var xhr = new XMLHttpRequest();
xhr.open("GET", "get_events.php", true);
xhr.onload = function() {
if (xhr.status == 200) {
// Parse JSON response
var events = JSON.parse(xhr.responseText);
// Add events to table
events.forEach(function(event) {
addEventToTable(event);
});
}
};
xhr.send();
}
// Attach form submit handler
document.getElementById("event-form").addEventListener("submit", handleFormSubmit);
// Load initial events
loadEvents();
```
最后,您需要创建一个名为 `get_events.php` 的 PHP 文件,该文件将从数据库中获取所有赛事并返回 JSON 数据。以下是 PHP 代码示例:
```php
<?php
// Connect to database
$mysqli = new mysqli("localhost", "username", "password", "starcraft");
// Check connection
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
exit();
}
// Get all events from database
$result = $mysqli->query("SELECT * FROM events ORDER BY date DESC");
// Create array to hold events
$events = array();
// Add events to array
while ($row = $result->fetch_assoc()) {
$events[] = array(
"title" => $row["title"],
"date" => date("F j, Y g:i a", strtotime($row["date"])),
"location" => $row["location"],
"description" => $row["description"]
);
}
// Close database connection
$mysqli->close();
// Send events as JSON response
header("Content-Type: application/json");
echo json_encode($events);
?>
```
现在您可以使用这些文件来创建一个星际争霸2赛事网站的赛事安排面板。请注意,这只是一个简单的示例,您可能需要根据自己的需求进行更改和扩展。
阅读全文