Jump to content

Array with content of multiple="multiple"


Muiter

Recommended Posts

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

 <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

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

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

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

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

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

$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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...