News portal script

Here is a basic script for a news portal in PHP and MySQL:

Database Schema:

Create a database named news_portal and create the following tables:

  1. categories:
    • id (primary key, auto-increment)
    • name (varchar)
    • description (text)
  2. articles:
    • id (primary key, auto-increment)
    • title (varchar)
    • content (text)
    • category_id (foreign key referencing categories.id)
    • author (varchar)
    • date_published (datetime)
  3. comments:
    • id (primary key, auto-increment)
    • article_id (foreign key referencing articles.id)
    • author (varchar)
    • comment (text)
    • date_commented (datetime)

PHP Script:

Create a PHP file named news_portal.php and add the following code:

<?php
// Configuration
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'news_portal';

// Connect to database
$conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);

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

// Function to get all categories
function get_categories() {
    $query = "SELECT * FROM categories";
    $result = mysqli_query($conn, $query);
    $categories = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $categories[] = $row;
    }
    return $categories;
}

// Function to get all articles
function get_articles() {
    $query = "SELECT * FROM articles";
    $result = mysqli_query($conn, $query);
    $articles = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $articles[] = $row;
    }
    return $articles;
}

// Function to get article by ID
function get_article($id) {
    $query = "SELECT * FROM articles WHERE id = '$id'";
    $result = mysqli_query($conn, $query);
    $article = mysqli_fetch_assoc($result);
    return $article;
}

// Function to add comment
function add_comment($article_id, $author, $comment) {
    $query = "INSERT INTO comments (article_id, author, comment) VALUES ('$article_id', '$author', '$comment')";
    mysqli_query($conn, $query);
}

// Function to get comments for an article
function get_comments($article_id) {
    $query = "SELECT * FROM comments WHERE article_id = '$article_id'";
    $result = mysqli_query($conn, $query);
    $comments = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $comments[] = $row;
    }
    return $comments;
}

// Display categories
$categories = get_categories();
?>

<!-- Display categories -->
<h1>Categories</h1>
<ul>
    <?php foreach ($categories as $category) {?>
        <li><a rel="nofollow" target="_blank" href="#"><?= $category['name']?></a></li>
    <?php }?>
</ul>

<!-- Display articles -->
<h1>Articles</h1>
<ul>
    <?php foreach (get_articles() as $article) {?>
        <li>
            <h2><?= $article['title']?></h2>
            <p><?= $article['content']?></p>
            <p>Published by <?= $article['author']?> on <?= $article['date_published']?></p>
            <a rel="nofollow" target="_blank" href="#"><?= $article['category_name']?></a>
            <ul>
                <?php foreach (get_comments($article['id']) as $comment) {?>
                    <li>
                        <p><?= $comment['author']?>: <?= $comment['comment']?></p>
                        <p>Commented on <?= $comment['date_commented']?></p>
                    </li>
                <?php }?>
            </ul>
        </li>
    <?php }?>
</ul>

<!-- Add comment form -->
<h1>Leave a comment</h1>
<form action="#" method="post">
    <label for="article_id">Article:</label>
    <select name="article_id">
        <?php foreach (get_articles() as $article) {?>
            <option value="<?= $article['id']?>"><?= $article['title']?></option>
        <?php }?>
    </select>
    <br>
    <label for="author">Author:</label>
    <input type="text" name="author" required>
    <br>
    <label for="comment">Comment:</label>
    <textarea name="comment" required></textarea>
    <br>
    <input type="submit" value="Submit">
</form>

<!-- Add comment script -->
<script>
    document.getElementById('comment-form').addEventListener('submit', function(event) {
        event.preventDefault();
        var articleId = document.querySelector('select[name="article_id"]').value;
        var author = document.querySelector('input[name="author"]').value;
        var comment = document.querySelector('textarea[name="comment"]').value;
        add_comment(articleId, author, comment);
        document.getElementById('comment-form').reset();
    });
</script>

This script displays a list of categories, articles, and comments. It also allows users to add comments to articles.

Note: This is a basic script and you should add more functionality, such as user authentication, validation, and error handling. Additionally, you should use prepared statements to prevent SQL injection attacks.