0

我想要达到的目标:

使用链式选择列表将数据插入数据库表(选项值取自数据库表)

要求:对于第一个选择列表(“tip_cheltuiala”),可用的选项值必须仅是已插入的行中未使用的选项值(可用选项为:选项 1、2 和 3;我已经插入带有选项 1 的行和 3,现在只有选项 2 必须可用)

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>';

以及该选择列表的功能:

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();

我有第一个问题:选择列表填充了选项 2(这是正确的),但也填充了选项 3 - 我不知道为什么。

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>';

以及该选择列表的函数(与函数 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. 最后一步:根据选择列表“mod_inchidere”中选择的选项,我需要返回一个与选择列表“mod_inchidre”中的选项相关的值(也存储在数据库中),并将该值放入输入字段中,所以用户可以(如果他愿意)修改该值。

在那一步,我不知道如何做到这一点。我可以将该值放在另一个选择列表中,但是:用户不能修改该值,也不是这样做的方法。

请帮帮我。

表结构

mi_categ_cheltuiala -> | 编号 | tip_cheltuiala | categorie_cheltuiala |

mi_mod_inchidere -> | 编号 | categorie_cheltuiala | mod_inchidere |

cheltuieli_mod_inchidere(我需要插入数据的表)-> | 编号 | tip_cheltuiala | categorie_cheltuiala | mod_inchidere | 价值 |

要获得我需要在输入字段中输入的值,我需要为字段“mod_inchidere”查询表“mi_categ_valoare”

mi_categ_valoare -> | 编号 | mod_inchidere | 价值 |

$_POST['id_categ_cheltuiala'] -> 解释:

通过 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>

然后我使用一个服务文件来调用方法 TipCheltuiala()

4

1 回答 1

0

老实说:非英语的命名约定让我头晕目眩:)

问题: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.

使用下面的单个查询来丢失tip_cheltuila

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

需要研究其他问题...

于 2013-01-30T07:40:38.787 回答