带有输入字段的链式选择列表 - 以及从数据库中获取的值

时间:2013-01-29 18:35:02

标签: php sql

我想要实现的目标:

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

要求:对于第一个选择列表(“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“中的选项相关的值(也存储在数据库中),并将这些值放在输入字段中,所以用户可以(如果他想)修改该值。

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

请帮助我。

LE

表格结构

mi_categ_cheltuiala - &gt; | id | tip_cheltuiala | categorie_cheltuiala |

mi_mod_inchidere - &gt; | id | categorie_cheltuiala | mod_inchidere |

cheltuieli_mod_inchidere (我需要插入数据的表格) - &gt; | id | tip_cheltuiala | categorie_cheltuiala | mod_inchidere | valoare |

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

mi_categ_valoare - &gt; | id | mod_inchidere | valoare |

$ _ POST ['id_categ_cheltuiala'] - &gt;解释

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

1 个答案:

答案 0 :(得分: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);

需要研究其他问题......