简体   繁体   English

匹配SQL列并将其解析为另一个SQL表

[英]Matching SQL columns and parsing them into another SQL table

I have an SQL table that contains a column named 'Make' and one named 'Model'. 我有一个SQL表,其中包含一个名为“ Make”的列和一个名为“ Model”的列。 Each row has its own 'Make' and 'Model'. 每行都有自己的“制作”和“模型”。

I currently have two select elements on my web page; 我的网页上目前有两个选择元素; one is for the 'Make' and one is for the 'Model'. 一种用于“制造”,一种用于“模型”。

I then have a list that displays all of the Makes and Models in the SQL database, using PDO and a while loop to display everyone. 然后,我有了一个列表,其中使用PDO和while循环显示所有人,从而显示SQL数据库中的所有品牌和型号。

I have managed to filter the list on the page using this simple jQuery code: 我已经设法使用以下简单的jQuery代码过滤页面上的列表:

<script>
    $(document).ready(function(){
  $('.form-control').change(function(){
    var make = $(this).val();
    if(make != 'make-any'){
      $('.makes').hide();
      $('.'+make).show();
      } else {
        $('.makes').show();
        }
    });
  });</script>

Basically if a user selects one of the options it will hide all of the li's that don't have the same class as the option value that has been selected. 基本上,如果用户选择一个选项,它将隐藏所有与所选选项值不具有相同类别的li。

So now you basically understand what I am trying to achieve I am now left with something a little more tricky. 因此,现在您基本上了解了我要实现的目标,而现在我有了一些棘手的问题。

I am now trying to associate Makes with Models so I'm not left with a select element with options that don't apply to certain Makes for example... if a user selects BMW I don't want Models displaying that are not a BMW in the 'Model' select element options such as a Mustang. 我现在正尝试将Makes与Models关联起来,这样我就不会剩下带有不适用于某些Makes的选项的select元素了……如果用户选择BMW,我不希望显示的模型不是BMW在“模型”中选择元素选项,例如野马。

I am aware I will have to use AJAX for this function however I first need a way to format my Makes and Models. 我知道我必须为此功能使用AJAX,但是我首先需要一种格式化我的品牌和型号的方法。

Here is the basic plan, I want to create a new table in my database, this table will have columns named after the Makes so for example: 这是基本计划,我想在数据库中创建一个新表,该表将具有以Makes命名的列,例如:

Audi, BMW, Chevrolet etc... Audi, BMW, Chevrolet等...

In these columns I will have each row as a Model so the column will be built like this: 在这些列中,我将每一行都作为模型,因此该列将按以下方式构建:

Audi
A1
A2
A3

So I am now left with the problem of taking the data from the first SQL table and inserting each column as a Make from the 'Make' column. 因此,现在剩下的问题是从第一个SQL表中获取数据,然后从“ Make”列中将每个列作为Make插入。 I then need to be able to insert all of the Models that are associated with each Make into each of the 'Make' columns. 然后,我需要能够将与每个Make关联的所有模型插入到每个“ Make”列中。

I will then be able to make an AJAX call for a certain column to display the right models. 然后,我将可以对特定列进行AJAX调用以显示正确的模型。

Any ideas how I might be able to do the SQL table process? 有什么想法可以执行SQL表过程吗?

I would choose this database structure: 我将选择以下数据库结构:

CREATE TABLE make (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name varchar(50) NOT NULL
);

CREATE TABLE model (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name varchar(50) NOT NULL
);

CREATE TABLE make_model (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    make_id INT NOT NULL,
    model_id INT NOT NULL
);

INSERT INTO make(name) VALUES ('Audi'), ('BMW'), ('Chevrolet');
INSERT INTO model(name) VALUES ('A1'),('A2'),('A3'),('B1'),('B2'),('C1');
INSERT INTO make_model(make_id, model_id) VALUES (1, 1), (1, 2), (1, 3), (2, 4), (2, 5), (3, 6);

A PHP script to return related models in JSON format: 一个PHP脚本,以JSON格式返回相关模型:

getmodels.php

<?PHP
require "dbconfig.php";

if(isset($_GET['make']) && intval($_GET['make']) > 0){
    $SQL = "SELECT model.id, model.name FROM model, make_model WHERE model.id = make_model.model_id AND make_model.make_id=".$_GET['make'];
} else {
    $SQL = "SELECT id, name FROM model";
}

$data = json_encode($db->query($SQL)->fetchAll(PDO::FETCH_ASSOC));
header("Content-Type: application/json");
echo $data;
?>

A make-model listing page like this 像这样的模型列表页面

<?PHP
require "dbconfig.php";
function list_options($tbl){
    global $db;
    $res = $db->query("SELECT * FROM $tbl");
    while($row = $res->fetch(PDO::FETCH_ASSOC)){
        echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
    }   
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Some form</title>
</head>
<body>
<form>
    <select id="make">
     <option value="0">All</option>
    <?PHP list_options("make"); ?>
    </select>
    <select id="model">
    <?PHP list_options("model"); ?>
    </select>
</form>
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(function(){
    $('#make').on('change', function(){
            $("#model").empty();
      $.getJSON("getmodels.php?make=" + this.selectedIndex, function(result){
        $.each(result, function(i, row){
            $("#model").append('<option value="'+row['id']+ '">'+row['name']+'</option>');
        });
      });
    });
});
</script>
</body>
</html>

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

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