goltoof Posted July 13, 2011 Share Posted July 13, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/ Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242378 Share on other sites More sharing options...
goltoof Posted July 13, 2011 Author Share Posted July 13, 2011 why can't you simply echo other keys? I can but I was wondering if there was another way to do it without having to echo each value individually. No way to just echo once and just list every value i want in one line? Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242380 Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 there are several ways to do this, however simply echoing the array elements is the simplest and shortest method for this specific instance Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242381 Share on other sites More sharing options...
goltoof Posted July 13, 2011 Author Share Posted July 13, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242385 Share on other sites More sharing options...
wildteen88 Posted July 13, 2011 Share Posted July 13, 2011 You can use a loop, such as a for loop $field_array_count = count($field_array); for($i = 1; $i < $field_array_count; $i++) { echo $field_array['field' . $i]; } However what you doing in each option? Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242401 Share on other sites More sharing options...
goltoof Posted July 13, 2011 Author Share Posted July 13, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242404 Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242409 Share on other sites More sharing options...
goltoof Posted July 13, 2011 Author Share Posted July 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242419 Share on other sites More sharing options...
AyKay47 Posted July 13, 2011 Share Posted July 13, 2011 there are a few examples in the link that I provided Quote Link to comment https://forums.phpfreaks.com/topic/241921-echo-multiple-array-values/#findComment-1242429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.