Muiter Posted April 5, 2010 Share Posted April 5, 2010 This is (part of) my form: <?php for ($i=0; $i<=4; $i++) { ?> <tr> <td> <?php echo $calculatie[$i]; ?> <?php if($error_aantal[$i] == "ja"){ $error_omschr = $error_omschr_aantal[$i]; include('includes/input_error.php'); } ?> <textarea rows="7" cols="80" name="omschrijving[]" /><?php echo $omschrijving[$i]; ?></textarea> </td> <td> <select multiple="multiple" width="50" size="7" name="calculatie[]" style="width: 150px"> <?php $sql_calc = "select id, naam from calculatie WHERE offerte_id = '".$row['off_offerte']."' ORDER BY id ASC"; $res_calc = mysql_query($sql_calc,$con); while ($row_calc = mysql_fetch_assoc($res_calc)){ ?> <option value="<?php echo $row_calc["id"] ?>"><?php if (empty($row_calc["naam"])) { echo $row_calc["id"]; } else { echo $row_calc["naam"]; } ?></option> <?php } ?></select> </td> </tr> <?php } ?> I have been trying with the array without succes. How to use it? Please help. With the code below it doesn't work. <?phpfor ($i=0; $i<=4; $i++) { $omschrijving[$i] = trim(mysql_real_escape_string($_POST['omschrijving'][$i])); foreach(mysql_real_escape_string($_POST['calculatie'][$i]) AS $calculatie_id[$i]); }?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 5, 2010 Share Posted April 5, 2010 You can just loop through that array normally. This syntax doesn't make sense:foreach(mysql_real_escape_string($_POST['calculatie'][$i]) AS $calculatie_id[$i])because mysql_real_escape_string($_POST['calculatie'][$i]) is not an array, it's a string (mysql_real_escape_string returns a string). $calculatie = $_POST['calculatie'];if (is_array($calculatie) && count($calculatie) > 0){ foreach ($calculatie as $val) { echo 'selected ' . $val . '<br>'; }}else{ echo 'no options selected';} Link to comment Share on other sites More sharing options...
Muiter Posted April 5, 2010 Author Share Posted April 5, 2010 Ok, but how does it connected to $omschrijving?$omschrijving can have different $calculatie. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 6, 2010 Share Posted April 6, 2010 If you're printing 5 sets of fields which need to relate to each other, and the select list needs to have a different array for each set of fields, then you need to set it up so that the select list is a multidimensional array, like this:name="calculatie[<?php echo $i; ?>][]"After that, $calculatie[0] will be an array of the choices for the first set, $calculatie[1] will be an array of the choices for the second set, etc. Link to comment Share on other sites More sharing options...
Muiter Posted April 6, 2010 Author Share Posted April 6, 2010 Thank you so far, to avoid any problems I have split my code up into 3 querys: <?php$sql = "INSERT INTO offerte_order_bev ( offerte_id, naam, actief, soort, door_gemaakt, datum_gemaakt, opmerkingen ) VALUES ( '".$offerte_id."', '".$naam."', 'ja', '".$soort."', '".$door_gemaakt."', NOW(), '".$opmerkingen."' )";$id_offerte_order_bev = mysql_insert_id();?> <?phpfor ($i=0; $i<=4; $i++){ if (!empty($omschrijving[$i])) { // voer query uit $sql_inh_omsch = "INSERT INTO sp_offerte_order_bev_omschr ( offerte_order_bev_id, omschrijving ) VALUES ( '".$id_offerte_order_bev."', '".$omschrijving[$i]."' )";?> <?phpfor ($i=0; $i<=4; $i++){ if (!empty($omschrijving[$i])) { $calculatie_id[$i] = array(); foreach ($calculatie_id[$i] AS $val) { // voer query uit $sql_inh_omsch = "INSERT INTO sp_offerte_order_bev_calc ( offerte_order_bev_id, calculatie_id ) VALUES ( '".$id_offerte_order_bev."', '".$val."' )"; }?> The last query is not working. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 6, 2010 Share Posted April 6, 2010 $calculatie_id[$i] = array();That line deletes whatever is already there and replaces it with an empty array. Link to comment Share on other sites More sharing options...
Muiter Posted April 7, 2010 Author Share Posted April 7, 2010 If I remove the code $calculatie_id[$i] = array(); then this error appears:Invalid argument supplied for foreach()When I use name="calculatie_id[<?php echo $i; ?>][]" then one more error appears:Cannot use a scalar value as an array Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2010 Share Posted April 7, 2010 How are you setting $calculatie_id in the PHP? You need to use the name like I showed, if that's producing an error then you're not retrieving the array correctly from $_POST. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.