Jump to content

$opts = array(1 => "opt1", "opt2", "opt3", "opt4");, how to have three fields


jasonc

Recommended Posts

Can someone please help me with an array problem i can not figure out.

 

I need the array to be numbered from 1 to how ever many fields that are needed in the form and have a mysql field name and the title of the field also in the array.

 

1, "mysql_field_name", "Title of form field"

2, "", ""

 

and so on

 

then the form will be shown based on the array.

 

I have the following draft code which I am working with.

 

any suggestions on how i may do this array ?

 

<?php
$options = array(
	'1'=>	array('fieldtext'=>'option1', 'mysqlfield'=>'option1'),
	'2'=>	array('fieldtext'=>'option2', 'mysqlfield'=>'option2'),
	'3'=>	array('fieldtext'=>'option3', 'mysqlfield'=>'option3'),
	'4'=>	array('fieldtext'=>'option4', 'mysqlfield'=>'option4'),
	);


// $options = array(1 => "option1", "option2", "option3", "option4");

// the line above works but i want to include the name of the mysql field as well.

$userid = 1;

?>
<div style="align: center; margin: 12px; font-family:Tahoma;">
<br><br><?php
if ($_POST['Update'] != "Update") { // check if form submitted yet, if not get data from mysql.
								$res = db_query("SELECT * FROM `users` WHERE `userid` = '" . $userid . "'");
								foreach($options as $key => $value) { $_POST[$key] = mysql_result($res, 0, $value); }
$ok_to_update = "no";
} elseif ($_POST['Update'] == "Update") { // check if form submitted yet, if so get POST data.
										// error checking
						//				foreach($options as $key => $value) {
						//					$_POST[$i] = "";
						//				}
$ok_to_update = "yes";
}

if ($_POST['Update'] == "Update" && $ok_to_update == "yes") {
							//	$res = db_query("INSERT INTO `users` () VALUES ()"); // add user details to database.
								?><p><br><br><br>Thank you for updating</p><?php
								} else {
										?><form name="form1" method="post" action="">
												<?php foreach($options as $key => $value) {
												?><p><?php echo($value); ?>: <input type="text" name="<?php echo($key); ?>" value="<?php echo($_POST[$key]);?>"></p>
												<?php } ?>
											<input name="Update" type="submit" value="Update">
										</form>
										<?php } ?>
</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.