简体   繁体   English

从多个表到div(HTML,PHP和MySQL)获取信息

[英]Getting information from multiple tables to a div (HTML,PHP and MySQL)

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. 有多种格式相同和不同的电影(vcd,dvd,vhs)。数据库中有两个表,一个表用于显示电影的标题,导演等一般信息,另一个表专门用于电影磁盘,例如字幕,语言,磁盘数量。

I created a div, when showing search results. 显示搜索结果时,我创建了一个div。 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. 只有电影名称作为搜索结果,并且在它们附近有一个展开按钮,单击该按钮时,页面右侧将显示一个信息div,显示电影中的所有信息。 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 ? 数据库功能和PHP代码将是什么?

I am open to all sorts of ideas or pieces of codes 我乐于接受各种想法或代码

Thanks 谢谢

The short answer is that $mysqli or $mysql returns multiple rows and you need to loop through them. 简短的答案是$ mysqli或$ mysql返回多行,您需要遍历它们。

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. 请注意,这并不是经过优化的(您可能希望在单个WHERE IN请求中获取所有电影的所有磁盘,然后分配它们。但这是使您入门的第一步。

Movie Table: 电影表:

id (PRIMARY KEY)
title
director

Disk Table 磁盘表

id (PRIMARY KEY)
film_id
subtitle
language

PHP: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM