0

I am working on a movie booking/viewing website for a particular archive. There are multiple movies in both same and different formats(vcd,dvd,vhs).There are two tables in the database, one is just for the general information like title, director etc. of the movie and the other table is special for the disk like subtitles, language, number of disks.

I created a div, when showing search results. Only the movie names came as the search result, and there is an expand button near them, when the button is clicked an information div appears on the right of the page showing all information on the movie. But, there is a problem with the multiple movies I mentioned above. I couldn’t manage to do it for multiple movies.

I want the page to show only one title for duplicate movies and when I click on expand button, I want it to show all of the details for all copies for duplicates(may be there could be a dropdown box, and the user selects the format then according to choice, the information is showed)

How can I do this? What will be the database functions and php code for this ?

I am open to all sorts of ideas or pieces of codes

Thanks

2
  • can you provide some sample code for us to wrap our heads around? Commented Dec 21, 2011 at 18:35
  • 1
    having duplicate movies indicates that you might want to reconsider your db schema. It might be better to seperate out movies, so there is only every 1 title. And instead the movie has a format field which would be a many-to-many relationship, this would get rid of your duplicates and better normalize your db structure. Commented Dec 21, 2011 at 18:43

1 Answer 1

1

The short answer is that $mysqli or $mysql returns multiple rows and you need to loop through them.

A decent initial approach might be. Note that this isn't optimised (you'd probably want to get all the disks for all the movies in a single WHERE IN request and then assign them. But this is a first go to get you started.

Movie Table:

id (PRIMARY KEY)
title
director

Disk Table

id (PRIMARY KEY)
film_id
subtitle
language

PHP:

class DB {
    private static $mysqli;
    public static function getDB() {
        if(!isset($this->mysqli)) {
            $this->mysqli = new mysqli('localhost','user','password','db');
        }
        return $this->mysqli();
    }
    public static function closeDB() {
        $mysqli->close();
        unset($mysqli);
    }
}
class Disk {
    private $id;
    private $film_id;
    private $subtitle;
    private $language;
    public static function GetByFilmId($id) {
        $id= DB::getDB()->real_escape_string($id);
        $query = sprintf("SELECT * FROM movie where title = '%i1'",$id);
        $result = DB::getDB()->query($query);
        $disks = array();
        while ($disk = $result->fetch_object("Disk")) {
            $disks[] = $disk;
        }
        $result->close();
        return $disks;
    }
    public render() {
?>
<div class="disk">
    <div><?php echo $this->subtitle; ?></div>
</div>
<?php
    }
}
class Movie {
    private $id;
    private $title;
    private $director;
    public static function GetByTitle($title) {
        $title = DB::getDB()->real_escape_string($title);
        $query = sprintf("SELECT * FROM movie where title = '%s1'",$title);
        $result = DB::getDB()->query($query);
        $movies = array();
        while ($movie = $result->fetch_object("Movie")) {
            $movies[] = $movie;
        }
        $result->close();
        return $movies;
    }
    public __construct() {
        $this->getDisks();
    }
    public getDisks() {
        $disks = Disks::GetByFilmId($this->id);
    }
    public render() {
?>
<div class="movie">
<div><?php echo $this->title; ?></div>
<div class="disks">       
<?php 
foreach($disks as $disk) {
   $disk->render();   
}
?>
</div>
<?php
    }
}
$movies = Movie::GetByTitle($_GET['title']);
DB::closeDB();
?>
<html>
<head>
    <title>MovieLand :: Movies Named <?php echo $_GET['title']; ?></title>
</head>
<body>
<h1>A List of Movies</h1>
<div class="movies">
<?php
foreach($movies as $movie) {
    $movie->render();
}
?>
</div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.