Jump to content

echo multiple array values


goltoof

Recommended Posts

For option1 I need to echo not just field1, but field2, field4, etc..  What's the proper way to do this?

<?php
$field_array = array(
'field1' => '<tr><td>Field 1:</td><td><input type="text" name="field1" /></td></tr>',
'field2' =>  '<tr><td>Field 2:</td><td><input type="text" name="field2" /></td></tr>',
'field3' =>  '<tr><td>Field 3:</td><td><input type="text" name="field3" /></td></tr>',
'field4' =>  '<tr><td>Field 4:</td><td><input type="text" name="field4" /></td></tr>',
'field5' =>  '<tr><td>Field 5:</td><td><input type="text" name="field5" /></td></tr>',
);
?>

<?php
if(isset($_GET["q"])){
?>
<form method="post" action="update.php">
	<table>
		<?
		switch ($_GET["q"]) {
			case "option1" :
				echo $field_array['field1'];
			break;
			case "option2" :
				echo $field_array['field1'];
			break;
			}
			?>
	</table>
	<input type="submit">
</form>
<?php
}
?>

Link to comment
Share on other sites

why can't you simply echo other keys?

 

<?php
$field_array = array(
'field1' => '<tr><td>Field 1:</td><td><input type="text" name="field1" /></td></tr>',
'field2' =>  '<tr><td>Field 2:</td><td><input type="text" name="field2" /></td></tr>',
'field3' =>  '<tr><td>Field 3:</td><td><input type="text" name="field3" /></td></tr>',
'field4' =>  '<tr><td>Field 4:</td><td><input type="text" name="field4" /></td></tr>',
'field5' =>  '<tr><td>Field 5:</td><td><input type="text" name="field5" /></td></tr>',
);
?>

<?php
if(isset($_GET["q"])){
?>
<form method="post" action="update.php">
	<table>
		<?php
		switch ($_GET["q"]) {
			case "option1" :
				echo $field_array['field1']. "<br />";
                                        echo $field_array['field2']; // etc.......
			break;
			case "option2" :
				echo $field_array['field1'];
			break;
			}
			?>
	</table>
	<input type="submit">
</form>
<?php
}
?>

Link to comment
Share on other sites

The actual script I'm working on involves 50 or so values to display  for each option, with 10 options, and each option has different values.  There are 200 values in total between the 10 options.

 

I think it's just a little daunting having to echo each array individually 50 times for each option, that's a lot of echos and makes for a very ugly script...

Link to comment
Share on other sites

However what you doing in each option?

 

I don't think a loop applies here since I'm not displaying every field for each option.  Each option contains up to 50 fields that differ from other options. 

 

option1 (field1, field2, field3, field4, field27, field28, field29, field56, field88, field89, etc)

option2 (field1, field2, field9, field10, field11, field23, field34, etc)

option3 (field1, field2 field3, field36, field37, field52, field56, etc)

Link to comment
Share on other sites

if you really want to use a loop, you can take what wildteen88 suggested or something like it, and incorporate the continue statement for the instances that you will not want to echo

 

I'll look into it.  An example as it pertains to the situation would be extremely helpful.  I'd like to see if it's at least more practical than inidividual echos.

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.