简体   繁体   English

具有输入字段的链式选择列表-值取自数据库

[英]chained select list with input field - and values taken from database

What I want to achieve: 我要实现的目标:

Insert data into database table using chained select list (the options values ​are ​taken from database table) 使用链式选择列表将数据插入数据库表(从数据库表中获取选项值)

Requirement: for the first select list ("tip_cheltuiala"), the available options values must be only the ones that are not used in the rows already inserted (available options are: option 1, 2 and 3; I already inserted rows with option 1 and 3, and now only option 2 must be available) 要求:对于第一个选择列表(“ tip_cheltuiala”),可用选项值必须仅是已插入行中未使用的选项值(可用选项为:选项1、2和3;我已经插入了带有选项1的行和3,现在只有选项2必须可用)

1. the select list "tip_cheltuiala": 1.选择列表“ tip_cheltuiala”:

echo '<select name="tip_cheltuiala'.$row_chelt['id_factura'].'" id="tip_cheltuiala'.$row_chelt['id_factura'].'" class="selectContentIncasare">'.$opt_mod_inchidere->TipCheltuiala().'</select>';

and the function for that select list: 以及该选择列表的功能:

class SelectListModInchidere{
public function TipCheltuiala(){
                    //looking for options that are  already in the table
        $stmt_chelt_inchisa = $this->conn->prepare('SELECT tip_cheltuiala FROM cheltuieli_mod_inchidere');
        $stmt_chelt_inchisa->execute(array());
        $results_chelt_inchisa = $stmt_chelt_inchisa->fetchAll();
        foreach($results_chelt_inchisa as $row_chelt_inchisa) {
            $chelt_inchisa[] = $row_chelt_inchisa['tip_cheltuiala'];
        }
        print_r($chelt_inchisa); // returns options 1 and 3

        for($i=0; $i < count($chelt_inchisa); $i++){    

            $stmt_tip_chelt = $this->conn->prepare('SELECT * FROM mi_categ_cheltuiala 
            WHERE tip_cheltuiala <> :chelt_inchisa');
            $stmt_tip_chelt->execute(array('chelt_inchisa' => $chelt_inchisa[$i]));
            $tip_cheltuiala = '<option value="0">selectati ...</option>';
            while($row_tip_chelt = $stmt_tip_chelt->fetch()) {
                $tip_cheltuiala .= '<option value="' . $row_tip_chelt['tip_cheltuiala'] . '">' . $row_tip_chelt['tip_cheltuiala'] . '</option>';
            }
            return $tip_cheltuiala;
        }
    }
}
$opt_mod_inchidere = new SelectListModInchidere();

There I have the first issue : the select list is populated with option 2 (that is correct) but also with option 3 - I can't figure out why. 我遇到的第一个问题是 :选择列表中填充了选项2(是正确的),但也填充了选项3-我不知道为什么。

2. the select list "mod_inchidere": returns the option values according with the selected option in select list "tip_cheltuiala 2.选择列表“ mod_inchidere”:根据选择列表“ tip_cheltuiala”中的所选选项返回选项值

echo '<select name="mod_inchidere'.$row_chelt['id_factura'].'" id="mod_inchidere'.$row_chelt['id_factura'].'" class="selectContentIncasare">
      <option value="0">selectati ...</option>
      </select>';

and the function for that select list (part of the same class as function TipCheltuiala): 以及该选择列表的功能(与TipCheltuiala函数属于同一类):

public function ModInchidere(){
        $stmt_mod_inch = $this->conn->prepare('SELECT * FROM mi_mod_inchidere WHERE categorie_cheltuiala = :categorie_cheltuiala');
        $stmt_mod_inch->execute(array('categorie_cheltuiala' => $_POST['id_categ_cheltuiala']));
        $mod_inchidere = '<option value="0">selectati ...</option>'; 
        while($row_mod_inch = $stmt_mod_inch->fetch()) {
            $mod_inchidere .= '<option value="' . $row_mod_inch['mod_inchidere'] . '">' . $row_mod_inch['mod_inchidere'] . '</option>';
        }
        return $mod_inchidere;
    }

3. final step: according with the selected option in select list "mod_inchidere, I need to return a value (also stored in database) correlated with the options in select list "mod_inchidre", and put that values in a input field, so the user can (if he wants) modify that value. 3.最后一步:根据选择列表“ mod_inchidere”中的选定选项,我需要返回一个与选择列表“ mod_inchidre”中的选项相关的值(也存储在数据库中),并将该值放入输入字段中,因此用户可以(如果需要)修改该值。

At that step I have no idea how to accomplish that. 在那一步,我不知道如何实现。 I can put the value in another select list, but: the user can't modify that value and is not the way to do it. 我可以将值放在另一个选择列表中,但是:用户无法修改该值,并且不是做到这一点的方法。

Please help me with that. 请帮我。

LE LE

table structures 表结构

mi_categ_cheltuiala -> | mi_categ_cheltuiala- > | id | id | tip_cheltuiala | tip_cheltuiala | categorie_cheltuiala | categorie_cheltuiala |

mi_mod_inchidere -> | mi_mod_inchidere- > | id | id | categorie_cheltuiala | categorie_cheltuiala | mod_inchidere | mod_inchidere |

cheltuieli_mod_inchidere (table where I need to insert the data) -> | cheltuieli_mod_inchidere (需要在其中插入数据的表)-> | id | id | tip_cheltuiala | tip_cheltuiala | categorie_cheltuiala | categorie_cheltuiala | mod_inchidere | mod_inchidere | valoare | 瓦洛雷|

to get the value that I need to put in the input field I need to interrogate the table "mi_categ_valoare" for the field "mod_inchidere" 为了获得我需要在输入字段中输入的值,我需要查询字段“ mod_inchidere”的表“ mi_categ_valoare”

mi_categ_valoare -> | mi_categ_valoare- > | id | id | mod_inchidere | mod_inchidere | valoare | 瓦洛雷|

$_POST['id_categ_cheltuiala'] -> explanation: $ _POST ['id_categ_cheltuiala']->说明:

through jQuery I fetch what is selected in this select list "tip_cheltuiala" and send the data to the method TipCheltuiala() 通过jQuery,我获取在此选择列表“ tip_cheltuiala”中选择的内容,并将数据发送到方法TipCheltuiala()

<script type="text/javascript">
$(document).ready(function(){
    $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").attr("disabled","disabled");
            $("select#tip_cheltuiala<?php echo $row_chelt['id_factura']; ?>").change(function(){
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").attr("disabled","disabled");
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").html("<option>asteptati ...</option>");
            var id_categ_cheltuiala = $("select#tip_cheltuiala<?php echo $row_chelt['id_factura']; ?> option:selected").attr('value');
            $.post("class/select_mod_inchidere.php", {id_categ_cheltuiala:id_categ_cheltuiala}, function(data){
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").removeAttr("disabled");
            $("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").html(data);
        });
    }); 
</script>

and then I use a service file that will invoke the method TipCheltuiala() 然后我使用一个服务文件,该文件将调用方法TipCheltuiala()

To be honest: Non-English naming conventions making me dizzy :) 老实说:非英语命名约定让我头晕:)

Issue: There I have the first issue: the select list is populated with option 2 (that is correct) but also with option 3 - I can't figure out why. 问题: There I have the first issue: the select list is populated with option 2 (that is correct) but also with option 3 - I can't figure out why.

use below single query to get missing tip_cheltuila : 使用以下单个查询来获取tip_cheltuila缺失的tip_cheltuila

SELECT 
    tip_cheltuila
FROM
    mi_categ_cheltuiala
WHERE
    tip_cheltuila NOT IN (SELECT 
            tip_cheltuiala
        FROM
            cheltuieli_mod_inchidere);

Need to study other issues... 需要研究其他问题...

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

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