News php template
Here is a basic PHP template for a news website:
index.php
<?php
// Database connection
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Get news articles
$query = "SELECT * FROM news ORDER BY date DESC";
$result = mysqli_query($conn, $query);
// Display news articles
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2>". $row['title']. "</h2>";
echo "<p>". $row['description']. "</p>";
echo "<p><a href='article.php?id=". $row['id']. "'>Read more</a></p>";
}
// Close database connection
mysqli_close($conn);
?>
article.php
<?php
// Database connection
$conn = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$conn) {
die("Connection failed: ". mysqli_connect_error());
}
// Get article by ID
$query = "SELECT * FROM news WHERE id = ". $_GET['id'];
$result = mysqli_query($conn, $query);
// Display article
while ($row = mysqli_fetch_assoc($result)) {
echo "<h1>". $row['title']. "</h1>";
echo "<p>". $row['content']. "</p>";
}
// Close database connection
mysqli_close($conn);
?>
style.css
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #00698f;
font-size: 24px;
margin-bottom: 10px;
}
h2 {
color: #333;
font-size: 18px;
margin-bottom: 10px;
}
p {
font-size: 16px;
margin-bottom: 20px;
}
a {
text-decoration: none;
color: #00698f;
}
a:hover {
color: #003366;
}
This template uses a simple database connection to retrieve news articles and display them on the index page. Each article is linked to a separate page (article.php) that displays the full article content.
You'll need to create a database and tables to store the news articles, and update the PHP code to match your specific database schema. You'll also need to create a style.css file to customize the appearance of the website.
This is just a basic example, and you may want to add additional features such as:
- User authentication and authorization
- Commenting system
- Search functionality
- Social media integration
- Responsive design