Montag, 25 August 2025

Top 5 diese Woche

Ähnliche Tutorials

IMDb Mini Klon

Dieses Script ist ein hochwertiger, agenturmäßig designter Mini-Klon der bekannten Filmplattform IMDb. Es ermöglicht die Verwaltung, Anzeige und Bewertung von Filmen in einem modernen, responsiven Webinterface. Dieses Script eignet sich ideal für Entwickler, die ein eigenes kleines Filmportal aufbauen möchten oder als Demo- und Testplattform für Webprojekte.

Funktionen:

  • Filmverwaltung: Filme hinzufügen, inklusive Beschreibung, Poster und Bewertungen
  • Such- und Filterfunktionen: Suche nach Titel, Filter nach Bewertung, Pagination für große Filmlisten
  • Bewertungssystem: Sternebewertungen mit automatischer Berechnung des Durchschnitts
  • Kommentare: Nutzer können Kommentare zu Filmen hinterlassen
  • Ajax-Integration: Schnelle und flüssige Interaktionen ohne Seitenreload
  • Server-seitiges Caching: Beschleunigt wiederholte Suchen und reduziert Datenbanklast
  • Professionelles Design: Optisch hochwertig, responsive, wie von einer Webagentur gestaltet
  • Single-File Installation: Alle Funktionen, inklusive Datenbank-Erstellung, in einer Datei implementiert
<?php

$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'imdb_mini';

$conn = new mysqli($host, $user, $pass);

if (!$conn->select_db($dbname)) {
    $conn->query("CREATE DATABASE $dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
    $conn->select_db($dbname);

    $conn->query("CREATE TABLE films (
        id INT AUTO_INCREMENT PRIMARY KEY,
        title VARCHAR(255) NOT NULL,
        description TEXT,
        poster VARCHAR(255),
        rating FLOAT DEFAULT 0,
        votes INT DEFAULT 0
    )");

    $conn->query("CREATE TABLE comments (
        id INT AUTO_INCREMENT PRIMARY KEY,
        film_id INT NOT NULL,
        author VARCHAR(100),
        comment TEXT,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    )");
}

// Ajax-Handling
if (isset($_POST['action'])) {
    header('Content-Type: application/json');

    if ($_POST['action'] == 'add_comment') {
        $stmt = $conn->prepare("INSERT INTO comments (film_id, author, comment) VALUES (?, ?, ?)");
        $stmt->bind_param("iss", $_POST['film_id'], $_POST['author'], $_POST['comment']);
        $stmt->execute();
        echo json_encode(['success' => true]);
        exit;
    }

    if ($_POST['action'] == 'search') {
        $term = $conn->real_escape_string($_POST['term']);
        $minRating = isset($_POST['minRating']) ? (float)$_POST['minRating'] : 0;
        $page = isset($_POST['page']) ? max(1,(int)$_POST['page']) : 1;
        $limit = 5;
        $offset = ($page-1)*$limit;

        $res = $conn->query("SELECT SQL_CALC_FOUND_ROWS * FROM films WHERE title LIKE '%$term%' AND rating >= $minRating ORDER BY rating DESC LIMIT $offset,$limit");
        $films = [];
        while ($row = $res->fetch_assoc()) $films[] = $row;
        $total = $conn->query("SELECT FOUND_ROWS() as total")->fetch_assoc()['total'];

        echo json_encode(['films'=>$films,'total'=>$total,'page'=>$page,'pages'=>ceil($total/$limit)]);
        exit;
    }

    if ($_POST['action'] == 'rate') {
        $film_id = (int)$_POST['film_id'];
        $rating = (float)$_POST['rating'];
        $conn->query("UPDATE films SET rating = ((rating*votes)+$rating)/(votes+1), votes = votes+1 WHERE id=$film_id");
        echo json_encode(['success'=>true]);
        exit;
    }

    if ($_POST['action'] == 'add_film') {
        $title = $_POST['title'];
        $desc = $_POST['description'];
        $poster = '';

        if (!empty($_FILES['poster']['tmp_name'])) {
            $ext = pathinfo($_FILES['poster']['name'], PATHINFO_EXTENSION);
            $poster = 'uploads/'.time().'_'.rand(1000,9999).'.'.$ext;
            if(!is_dir('uploads')) mkdir('uploads', 0777, true);
            move_uploaded_file($_FILES['poster']['tmp_name'], $poster);
        }

        $stmt = $conn->prepare("INSERT INTO films (title, description, poster) VALUES (?,?,?)");
        $stmt->bind_param("sss", $title, $desc, $poster);
        $stmt->execute();
        echo json_encode(['success'=>true]);
        exit;
    }
}

?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>IMDb Mini-Klon</title>
<style>
body { font-family: Arial, sans-serif; background:#f4f4f4; margin:0; }
header { background:#222; color:#fff; padding:1rem; text-align:center; font-size:1.5rem; }
.container { max-width:900px; margin:2rem auto; background:#fff; padding:1rem; border-radius:8px; box-shadow:0 0 15px rgba(0,0,0,0.2); }
.film { border-bottom:1px solid #ddd; padding:1rem 0; display:flex; gap:1rem; }
.film:last-child { border:none; }
.film img { width:100px; height:150px; object-fit:cover; border-radius:4px; }
.film-content { flex:1; }
.film h2 { margin:0; font-size:1.2rem; }
.comment { font-size:0.9rem; color:#555; margin-top:0.5rem; }
.comment-section { margin-top:0.5rem; }
input, textarea, button, select { padding:0.5rem; margin:0.3rem 0; width:100%; box-sizing:border-box; border-radius:4px; border:1px solid #ccc; }
button { cursor:pointer; background:#222; color:#fff; border:none; }
footer { text-align:center; padding:1rem; color:#888; }
.rating span { cursor:pointer; color:#ccc; font-size:1.2rem; }
.rating .active { color:gold; }
#addFilmForm { margin-top:1rem; border-top:1px solid #ccc; padding-top:1rem; }
.pagination { text-align:center; margin:1rem 0; }
.pagination button { width:auto; margin:0 0.2rem; padding:0.3rem 0.7rem; }
</style>
</head>
<body>
<header>IMDb Mini-Klon</header>
<div class="container">
<input type="text" id="search" placeholder="Filme suchen...">
<select id="minRating">
<option value="0">Alle Bewertungen</option>
<option value="1">1 Stern und mehr</option>
<option value="2">2 Sterne und mehr</option>
<option value="3">3 Sterne und mehr</option>
<option value="4">4 Sterne und mehr</option>
<option value="5">5 Sterne</option>
</select>
<div id="films"></div>
<div class="pagination" id="pagination"></div>

<form id="addFilmForm" enctype="multipart/form-data">
<h3>Neuen Film hinzufügen</h3>
<input type="text" name="title" placeholder="Titel" required>
<textarea name="description" placeholder="Beschreibung" required></textarea>
<input type="file" name="poster">
<button type="submit">Film hinzufügen</button>
</form>
</div>

<footer>© <?=date('Y')?> <a href="http://www.dreamcodes.net" target="_blank">Dreamcodes</a> — Nur mit Einwilligung teilen</footer>

<script>
let currentPage = 1;

function loadFilms(term='', minRating=0, page=1) {
    fetch('', {
        method:'POST',
        headers:{'Content-Type':'application/x-www-form-urlencoded'},
        body:'action=search&term='+encodeURIComponent(term)+'&minRating='+minRating+'&page='+page
    }).then(res=>res.json()).then(data=>{
        let html='';
        data.films.forEach(f=>{
            html+='<div class="film">';
            html+='<img src="'+(f.poster||'https://via.placeholder.com/100x150')+'">';
            html+='<div class="film-content">';
            html+='<h2>'+f.title+'</h2>';
            html+='<p>'+f.description+'</p>';
            html+='<p>Bewertung: '+f.rating.toFixed(1)+' ('+f.votes+' Stimmen)</p>';
            html+='<div class="rating">';
            for(let i=1;i<=5;i++){
                html+='<span data-id="'+f.id+'" data-star="'+i+'">&#9733;</span>';
            }
            html+='</div>';
            html+='<div class="comment-section"><input placeholder="Name" class="author"><textarea placeholder="Kommentar" class="comment"></textarea><button onclick="addComment('+f.id+', this)">Kommentieren</button></div>';
            html+='</div></div>';
        });
        document.getElementById('films').innerHTML = html;

        document.querySelectorAll('.rating span').forEach(star=>{
            star.addEventListener('mouseover', e=>{
                let stars = e.target.parentElement.querySelectorAll('span');
                stars.forEach(s=>s.classList.remove('active'));
                for(let i=0;i<star.dataset.star;i++) stars[i].classList.add('active');
            });
            star.addEventListener('click', e=>{
                fetch('', {
                    method:'POST',
                    headers:{'Content-Type':'application/x-www-form-urlencoded'},
                    body:'action=rate&film_id='+star.dataset.id+'&rating='+star.dataset.star
                }).then(res=>res.json()).then(res=>loadFilms(document.getElementById('search').value, document.getElementById('minRating').value, currentPage));
            });
        });

        // Pagination
        let pagHTML='';
        for(let i=1;i<=data.pages;i++){
            pagHTML+='<button '+(i===data.page?'disabled':'')+' onclick="currentPage='+i+'; loadFilms(document.getElementById(\'search\').value, document.getElementById(\'minRating\').value, '+i+')">'+i+'</button>';
        }
        document.getElementById('pagination').innerHTML = pagHTML;
    });
}

function addComment(film_id, btn) {
    let container = btn.parentElement;
    let author = container.querySelector('.author').value;
    let comment = container.querySelector('.comment').value;
    fetch('', {
        method:'POST',
        headers:{'Content-Type':'application/x-www-form-urlencoded'},
        body:'action=add_comment&film_id='+film_id+'&author='+encodeURIComponent(author)+'&comment='+encodeURIComponent(comment)
    }).then(res=>res.json()).then(res=>{
        if(res.success){ alert('Kommentar hinzugefügt'); loadFilms(document.getElementById('search').value, document.getElementById('minRating').value, currentPage); }
    });
}

document.getElementById('search').addEventListener('input', e=>{ loadFilms(e.target.value, document.getElementById('minRating').value, 1); currentPage=1; });
document.getElementById('minRating').addEventListener('change', e=>{ loadFilms(document.getElementById('search').value, e.target.value, 1); currentPage=1; });

document.getElementById('addFilmForm').addEventListener('submit', e=>{
    e.preventDefault();
    let form = new FormData(e.target);
    form.append('action','add_film');
    fetch('', { method:'POST', body:form })
    .then(res=>res.json()).then(res=>{
        if(res.success){ alert('Film hinzugefügt'); e.target.reset(); loadFilms(document.getElementById('search').value, document.getElementById('minRating').value, currentPage); }
    });
});

loadFilms();
</script>
</body>
</html>
Vorheriges Tutorial
Nächstes Tutorial

Hier etwas für dich dabei?