0
votes

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)

1. the select list "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. the select list "mod_inchidere": returns the option values according with the selected option in select list "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):

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.

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

table structures

mi_categ_cheltuiala -> | id | tip_cheltuiala | categorie_cheltuiala |

mi_mod_inchidere -> | id | categorie_cheltuiala | mod_inchidere |

cheltuieli_mod_inchidere (table where I need to insert the data) -> | id | tip_cheltuiala | categorie_cheltuiala | 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"

mi_categ_valoare -> | id | mod_inchidere | valoare |

$_POST['id_categ_cheltuiala'] -> explanation:

through jQuery I fetch what is selected in this select list "tip_cheltuiala" and send the data to the method 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()

1
Can you share the table schema for all the involved tables? tip_cheltuiala, mi_categ_cheltuiala and mi_mod_inchidere - SparKot
see LE in the original post - bogdA
for using $_POST['id_categ_cheltuiala'], how did user got access to categorie_cheltuiala? - SparKot
I use ajax call to fetch the data. I'm on the road and I'll get back later with the script. - bogdA
@ SparKot - see the edit on original post for $_POST['id_categ_cheltuiala'] - bogdA

1 Answers

0
votes

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.

use below single query to get missing 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...