Muiter Posted April 22, 2010 Share Posted April 22, 2010 I've been trying for days now to get this to work.For some reason when I use print_r($calculatie_id) it only displays one of the items or just the word Array <?php <select multiple="multiple" width="50" size="7" name="calculatie_id" style="width: 150px"> <?php $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' 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>?> Please help. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 22, 2010 Share Posted April 22, 2010 Name it like an array:name="calculatie_id[]" Link to comment Share on other sites More sharing options...
thescientist Posted April 22, 2010 Share Posted April 22, 2010 edit: wrong thread Link to comment Share on other sites More sharing options...
Muiter Posted April 22, 2010 Author Share Posted April 22, 2010 Name it like an array:name="calculatie_id[]"That gives me the word Array when using print_r. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 22, 2010 Share Posted April 22, 2010 print_r won't just write Array and nothing else, is Array followed by parentheses? Link to comment Share on other sites More sharing options...
Muiter Posted April 22, 2010 Author Share Posted April 22, 2010 is Array followed by parentheses?No, nothing. Only the word Array Link to comment Share on other sites More sharing options...
jeffman Posted April 22, 2010 Share Posted April 22, 2010 Something's not right. Post your new select tag. Do you have a link?try var_dump($_POST) Link to comment Share on other sites More sharing options...
Muiter Posted April 22, 2010 Author Share Posted April 22, 2010 <select multiple="multiple" width="50" size="7" name="calculatie_id[]" style="width: 150px"> <?php $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' 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> var_dump($_POST) gives:["calculatie_id"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "4" [2]=> string(1) "5" } Link to comment Share on other sites More sharing options...
jeffman Posted April 22, 2010 Share Posted April 22, 2010 Ah. That looks correct. Are you trying to get individual values from calculatie_id or from calculatie_id[0], calculatie_id[1], and so on? You'll need the array indexes to get individual values. Or a foreach type structure. Link to comment Share on other sites More sharing options...
Muiter Posted April 22, 2010 Author Share Posted April 22, 2010 Ok, when I try: foreach ($calculatie_id as $value) { echo $value; } This error occurs: Warning: Invalid argument supplied for foreach() Link to comment Share on other sites More sharing options...
ShadowMage Posted April 22, 2010 Share Posted April 22, 2010 Did you set the variable $calculatie_id equal to the $_POST variable? $calculatie_id = $_POST['calculatie_id'];foreach ($calculatie_id as $value){ echo $value;} Link to comment Share on other sites More sharing options...
Muiter Posted April 22, 2010 Author Share Posted April 22, 2010 Did you set the variable $calculatie_id equal to the $_POST variable?Yes, I have.$calculatie_id = $_POST['calculatie_id']; Hmm, I ckecked it and now it works. Maybe an typo.Thanks for helping me out guys Link to comment Share on other sites More sharing options...
Muiter Posted April 26, 2010 Author Share Posted April 26, 2010 It works perfect now. The next step is to put everyting in an for loop.How can I use this piece of code in an for loop? <select multiple="multiple" width="50" size="7" name="calculatie_id[]" style="width: 150px"> <?php $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' ORDER BY id ASC"; $res_calc = mysql_query($sql_calc,$con); while ($row_calc = mysql_fetch_assoc($res_calc)){ ?> <option <?php if (in_array($row_calc["id"], $calculatie_id)){ echo 'selected="selected"'; } ?> value="<?php echo $row_calc["id"] ?>"><?php if (empty($row_calc["naam"])) { echo $row_calc["id"]; } else { echo $row_calc["naam"]; } ?></option> <?php } ?></select> Link to comment Share on other sites More sharing options...
ShadowMage Posted April 26, 2010 Share Posted April 26, 2010 How can I use this piece of code in an for loop?Depends on what you want the loop to do. Link to comment Share on other sites More sharing options...
Muiter Posted April 27, 2010 Author Share Posted April 27, 2010 Something like: <?php for ($i=0; $i<=5; $i++) { ?> <select multiple="multiple" width="50" size="7" name="calculatie_id[]" style="width: 150px"> <?php $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' ORDER BY id ASC"; $res_calc = mysql_query($sql_calc,$con); while ($row_calc = mysql_fetch_assoc($res_calc)){ ?> <option <?php if (in_array($row_calc["id"], $calculatie_id)){ echo 'selected="selected"'; } ?> value="<?php echo $row_calc["id"] ?>"><?php if (empty($row_calc["naam"])) { echo $row_calc["id"]; } else { echo $row_calc["naam"]; } ?></option> <?php } ?></select> <?php } ?> Link to comment Share on other sites More sharing options...
ShadowMage Posted April 27, 2010 Share Posted April 27, 2010 I can't see any reason why that shouldn't work. Link to comment Share on other sites More sharing options...
ShadowMage Posted April 27, 2010 Share Posted April 27, 2010 I can't see any reason why that shouldn't work. Did you try it? Link to comment Share on other sites More sharing options...
Muiter Posted April 27, 2010 Author Share Posted April 27, 2010 I have changed my code to: <?php for ($i=0; $i<=$aantal_regels_corr; $i++) { ?> <tr> <td> <?php if($error_vergeten[$i] == "ja"){ $error_omschr = $error_omschr_vergeten[$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_id[]" style="width: 150px"> <?php $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' ORDER BY id ASC"; $res_calc = mysql_query($sql_calc,$con); while ($row_calc = mysql_fetch_assoc($res_calc)){ ?> <option <?php if (!empty ($calculatie_id) && in_array($row_calc["id"], $calculatie_id)){ echo 'selected="selected"'; } ?> 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 } ?> Now there is one array filled with the items selected in the complete for loop. I need for each one in the for loop an seperate array. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 27, 2010 Share Posted April 27, 2010 You need to give them different names. Link to comment Share on other sites More sharing options...
Muiter Posted April 27, 2010 Author Share Posted April 27, 2010 You need to give them different names.I have tried:<select multiple="multiple" width="50" size="7" name="calculatie_id<?php $i ?>[]" style="width: 150px"> <?php $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' ORDER BY id ASC"; $res_calc = mysql_query($sql_calc,$con); while ($row_calc = mysql_fetch_assoc($res_calc)){ ?> <option <?php if (!empty ($calculatie_id.$i) && in_array($row_calc["id"], $calculatie_id.$i)){ echo 'selected="selected"'; } ?> value="<?php echo $row_calc["id"] ?>"><?php if (empty($row_calc["naam"])) { echo $row_calc["id"]; } else { echo $row_calc["naam"]; } ?></option> <?php } ?></select> And for ($i=0; $i<=$aantal_regels; $i++) { $omschrijving[$i] = trim(mysql_real_escape_string($_POST['omschrijving'][$i])); $calculatie_id.$i = $_POST['calculatie_id<?php $i ?>']; } This gives an error in: <?php if (!empty ($calculatie_id.$i) && in_array($row_calc["id"], $calculatie_id.$i)){ echo 'selected="selected"'; } ?> When removing this line the page won't load when submitting the form, error with executione time. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 27, 2010 Share Posted April 27, 2010 $calculatie_id.$i = $_POST['calculatie_id<?php $i ?>'];There are a couple things wrong with that line. First, on the left side of the equal sign, you can't have the dot operator. The left side of an equal sign needs to be a variable that it can assign the value on the right side to. You don't have a variable on the left side, you have a string, since you're using the string concatenation operator. You can't assign a value to a string, a string is already a value. That's the same thing as doing this:"some string" = 123;It just doesn't make sense to do that. You can't assign a value to another value, only to a variable.If you're trying to build a variable name as a string and then assign a value to a variable with that name, that's called a variable variable.http://www.php.net/manual/en/language.variables.variable.phpYou can use 2 dollar signs to tell it that you're trying to access a variable where the name is stored in another variable.The second thing wrong with that line is this part:$_POST['calculatie_id<?php $i ?>']This is already PHP code, you don't put opening PHP tags inside PHP code. You used the string concatenation operator on the left side, but this part is where you should be using it. Link to comment Share on other sites More sharing options...
Muiter Posted April 28, 2010 Author Share Posted April 28, 2010 Ok thanks for this explanation, now I tried: <?php for ($i=0; $i<=$aantal_regels_corr; $i++) { $calculatie_id = 'calculatie_id'.$i; ?> with <select multiple="multiple" width="50" size="7" name="<?php echo $calculatie_id ?>[]" style="width: 150px"> <?php $sql_calc = "select id, naam, actief from sp_calculatie WHERE (offerte_id = '".$row['off_offerte']."' OR offerte_id = '".$offerte_id."') AND actief = 'ja' 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> and for ($i=0; $i<=$aantal_regels; $i++) { $omschrijving[$i] = trim(mysql_real_escape_string($_POST['omschrijving'][$i])); $calculatie_id = $calculatie_id.$i; $calculatie_id[$i] = $_POST['calculatie_id'][$i]; } No errors nut no result. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 28, 2010 Share Posted April 28, 2010 What names are you seeing printed in the HTML for the different select elements? You're still checking for an element named "calculatie_id", but I don't think that's what you're naming the select elements. Link to comment Share on other sites More sharing options...
Muiter Posted April 28, 2010 Author Share Posted April 28, 2010 What names are you seeing printed in the HTML for the different select elements?I see:calculatie_id0[]calculatie_id1[]calculatie_id2[]because of:<?php for ($i=0; $i<=$aantal_regels_corr; $i++) { $calculatie_id = 'calculatie_id'.$i; ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted April 28, 2010 Share Posted April 28, 2010 Right, so those are the names you should be looking for in $_POST. You're still only looking for "calculatie_id":$calculatie_id[$i] = $_POST['calculatie_id'][$i];You could do something like this: $id_lists = array();for ($i = 0; $i < 5; $i++){ $id_lists[$i] = $_POST['calculatie_id' . $i];}print_r($id_lists); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.