Add user input into foreach loop


#1

I’ve inherited a site that holds a form to insert values into a Mysql db. It works fine, but I’ve been asked to add a field and can’t seem to work it out.

The site uses a form where users can check a box, and than add values to that row.
So we have a table:

LangID DepName Output

that holds several language codes (BE; EN, FR, DE,…), department codes and output formats. (e.g. NL - DANL - PDF or UK - DAB - Word)

On the form, a table is displayed for all languages with a checkbox and two dropdown lists. When the checkbox is checked, a row needs to be created for the language with the values in the dropdown lists.
At the moment, I have the languages and checkboxes with two select boxes, which works fine.
The code for this:
[PHP]

<?php $rowCount=-1; foreach( $languages as $key=>$value ){ if( isset( $_SESSION['formvalues']['addEntry']['target'] ) ){ if( in_array( $value, $_SESSION['formvalues']['addEntry']['target'] ) ) $selected = " checked=\"checked\""; else $selected = ""; }
							if( $rowCount == 0 ){
								echo "</tr>\n<tr>";
								$rowCount = 0;
							}else
								$rowCount++;
					?>
						<td align="right">
                        <?php echo $value; ?><input type="checkbox" name="target[]" value=<?php echo "\"".$value."\"" . $selected; ?> />
									 
						<select name="department_<?php echo $value ?>">	
							<?php
							$sqlDepartments = "SELECT depName FROM tbldepartements WHERE langId = '".trim($value)."'";
							$departments = mysql_query( $sqlDepartments ) or die( mysql_error() );
								while( $department = mysql_fetch_object( $departments ) ): ?>
									<?php
										$curDepartment = $_SESSION['formvalues']['addEntry']['department'][$value];
											if(  $curDepartment == utf8_encode($department->depName) )
												$optionSelected = ' selected="selected"';
												
											else
												$optionSelected = ''; 
									?>	 											   										   
										<option value="<?php echo utf8_encode($department->depName); ?>"<?php echo $optionSelected; ?>><?php echo utf8_encode($department->depName); ?></option>
									<?php endwhile; ?>
										<option value=""></option>
						 </select>
																		
						<select name="Output_<?php echo $value ?>">			
						<?php
						$sqlformats = "SELECT Output FROM tbldepartements WHERE langId = '".trim($value)."'";
						$formats = mysql_query( $sqlformats ) or die( mysql_error() );
							while( $format = mysql_fetch_object( $formats ) ): ?>
								<?php
									$curformat = $_SESSION['formvalues']['addEntry']['Output'][$value];
										if(  $curformat == utf8_encode($format->Output) )
											$optionSelected2 = ' selected="selected"';
											
										else
											$optionSelected2 = ''; 
								?>	 											   										   
									<option value="<?php echo utf8_encode($format->Output); ?>"<?php echo $optionSelected2; ?>><?php echo utf8_encode($format->Output); ?></option><span  class="error">&nbsp;&laquo;</span>
									
								<?php endwhile; ?>
									<option value=""></option>
						</select>
				                  
				<?php
						}
				?>				   												
						</tr>
				</table>

[/PHP]

This creates a row for each language in the DB on the form, with two dropdown lists for each language. When a language is checked, all values are inserted into a new row in the DB.

Language  Checkbox  Department(dropdown) Output(dropdown)

The values get stored in SESSION (as they are carried to a preview page, and from there to a page that does the insert into mysql)

I’m now trying to add a third field to the row with user input. I’ve tried
[PHP]
]


<?php
if(isset( $_SESSION[‘formvalues’][‘addEntry’][‘amount’] ) )
$amount= “value=”" . $_SESSION[‘formvalues’][‘addEntry’][‘amount’] . “”";
else
$amount = “”;
?> [/PHP but I'm unable to link the language to the user input field.

If I put the input field outside the off the table, it works. The session value is set and I can insert that value into the DB.
But when I try to set it for each language checked, it doesn’t carry.

How should I normally insert this user input in a foreach loop into a variable that can get processed on another page?

Alos note, I have limited PHP knowledge, and this is a bit much for me :slight_smile:

Kind regards, and if you need more info, let me know and I’ll supply all the needed info.

If I

amount />

#2

[php]

[/php]

#3

[php]

amount
<?php
if(isset( $_SESSION[‘formvalues’][‘addEntry’][‘amount’] ) ) {
$amount= “$_SESSION[‘formvalues’][‘addEntry’][‘amount’]”;
}else{
$amount = " ";
}
?> [/php]

this is what i found you did wrong from a quick view :slight_smile:
maybe someoane has time to look over your first script,
i`m going to bed now :slight_smile: