Crime news portal in php

Here is a basic outline of a crime news portal in PHP:

Database Design

Create a database with the following tables:

  1. news: stores news articles
    • id (primary key)
    • title
    • content
    • date
    • category (e.g. crime, politics, sports)
  2. categories: stores categories for news articles
    • id (primary key)
    • name
  3. tags: stores tags for news articles
    • id (primary key)
    • name
  4. news_tags: stores the many-to-many relationship between news articles and tags
    • news_id (foreign key referencing the news table)
    • tag_id (foreign key referencing the tags table)

PHP Code

Create a PHP script to interact with the database and display the crime news portal:

index.php

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

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

// Get all news articles
$query = "SELECT * FROM news WHERE category='crime'";
$result = mysqli_query($conn, $query);

// Display news articles
while ($row = mysqli_fetch_assoc($result)) {
    echo "<h2>". $row['title']. "</h2>";
    echo "<p>". $row['content']. "</p>";
    echo "<p>Category: ". $row['category']. "</p>";
    echo "<p>Tags: ";
    $tags = get_tags($row['id']);
    foreach ($tags as $tag) {
        echo $tag['name']. " ";
    }
    echo "</p>";
    echo "<hr>";
}

// Function to get tags for a news article
function get_tags($news_id) {
    $query = "SELECT t.name FROM news_tags nt JOIN tags t ON nt.tag_id=t.id WHERE nt.news_id=$news_id";
    $result = mysqli_query($conn, $query);
    $tags = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $tags[] = $row['name'];
    }
    return $tags;
}

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

news.php

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

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

// Get the news article ID from the URL
$news_id = $_GET['id'];

// Get the news article
$query = "SELECT * FROM news WHERE id=$news_id";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

// Display the news article
echo "<h2>". $row['title']. "</h2>";
echo "<p>". $row['content']. "</p>";
echo "<p>Category: ". $row['category']. "</p>";
echo "<p>Tags: ";
$tags = get_tags($news_id);
foreach ($tags as $tag) {
    echo $tag. " ";
}
echo "</p>";

// Function to get tags for a news article
function get_tags($news_id) {
    $query = "SELECT t.name FROM news_tags nt JOIN tags t ON nt.tag_id=t.id WHERE nt.news_id=$news_id";
    $result = mysqli_query($conn, $query);
    $tags = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $tags[] = $row['name'];
    }
    return $tags;
}

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

tags.php

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

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

// Get the tag name from the URL
$tag_name = $_GET['name'];

// Get the news articles with the tag
$query = "SELECT * FROM news WHERE id IN (SELECT news_id FROM news_tags WHERE tag_id=(SELECT id FROM tags WHERE name='$tag_name'))";
$result = mysqli_query($conn, $query);

// Display the news articles
while ($row = mysqli_fetch_assoc($result)) {
    echo "<h2>". $row['title']. "</h2>";
    echo "<p>". $row['content']. "</p>";
    echo "<p>Category: ". $row['category']. "</p>";
    echo "<p>Tags: ";
    $tags = get_tags($row['id']);
    foreach ($tags as $tag) {
        echo $tag. " ";
    }
    echo "</p>";
    echo "<hr>";
}

// Function to get tags for a news article
function get_tags($news_id) {
    $query = "SELECT t.name FROM news_tags nt JOIN tags t ON nt.tag_id=t.id WHERE nt.news_id=$news_id";
    $result = mysqli_query($conn, $query);
    $tags = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $tags[] = $row['name'];
    }
    return $tags;
}

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

categories.php

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

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

// Get the category name from the URL
$category_name = $_GET['name'];

// Get the news articles with the category
$query = "SELECT * FROM news WHERE category='$category_name'";
$result = mysqli_query($conn, $query);

// Display the news articles
while ($row = mysqli_fetch_assoc($result)) {
    echo "<h2>". $row['title']. "</h2>";
    echo "<p>". $row['content']. "</p>";
    echo "<p>Category: ". $row['category']. "</p>";
    echo "<p>Tags: ";
    $tags = get_tags($row['id']);
    foreach ($tags as $tag) {
        echo $tag. " ";
    }
    echo "</p>";
    echo "<hr>";
}

// Function to get tags for a news article
function get_tags($news_id) {
    $query = "SELECT t.name FROM news_tags nt JOIN tags t ON nt.tag_id=t.id WHERE nt.news_id=$news_id";
    $result = mysqli_query($conn, $query);
    $tags = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $tags[] = $row['name'];
    }
    return $tags;
}

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

This is a basic outline of a crime news portal in PHP. You will need to modify the code to fit your specific needs and add additional functionality as required.