hotrod57 Posted October 24, 2007 Share Posted October 24, 2007 I have a text file that I read into an array using explode(). My separator is =. I need to be able to look at the contents of $array[0] and $array[1] and create a form based on what is in the array. For example: $array[0] = checkbox, checkbox, radio button and $array[1] = shoe, hat, car. I need to be able to recognize that when I see the second "checkbox" in $array[0] it corresponds with "hat" in $array[1], and so on. I know this seems simple, but the catch is the values in the text file are changeable, I need my function to be able to recognize the differences. This time checkbox and shoe go together, but next time it could be a drop box with different values. Does this make sense? Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/ Share on other sites More sharing options...
Wuhtzu Posted October 24, 2007 Share Posted October 24, 2007 Could you please post some sample lines from your text file? Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-377390 Share on other sites More sharing options...
hotrod57 Posted October 24, 2007 Author Share Posted October 24, 2007 <?php $lines = file('test_read.txt'); foreach ($lines as $line_num => $line) { list($type, $value) = explode("=", $line); if ($type == "checkbox"){ print ("<input type=checkbox name=userbox value=$value>$value"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-377391 Share on other sites More sharing options...
Psycho Posted October 24, 2007 Share Posted October 24, 2007 Your original post and the sample code do not appear to make sense to me. In your first post you state that you are using the equal sign as a delimter and that the values of the array, after using explode, would be something like: $array[0] = checkbox, checkbox, radio button $array[1] = shoe, hat, car. But according to your code above you are not delimiting those based upon the commas. Try this: <?php $lines = file('test_read.txt'); foreach ($lines as $line) { list($fields, $values) = explode("=", $line); $fields = explode(",", $fields); $values = explode(",", $values); for ($i=0; $i<count($fields); $i++) { switch(trim($fields[$i])) { case 'checkbox': print ("<input type=\"checkbox\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i]).""); break; case 'radio': print ("<input type=\"radio\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i]).""); break; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-377401 Share on other sites More sharing options...
hotrod57 Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks a lot! That helped me out big time. But now I have the two checkboxes with no values beside them. Any suggestions? <?php $lines = file('test_read.txt'); foreach ($lines as $line) { list($fields, $values) = explode("=", $line); $fields = explode(",", $fields); $values = explode(",", $values); for ($i=0; $i<count($fields); $i++) { switch(trim($fields[$i])) { case 'checkbox': print ("<input type=\"checkbox\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i]).""); break; case 'radio': print ("<input type=\"radio\" name=\"userbox\" value=\"".trim($value[$i])."\">".trim($value[$i]).""); break; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-377406 Share on other sites More sharing options...
Psycho Posted October 25, 2007 Share Posted October 25, 2007 Can you show some of the data EXACTLY as it exists in the txt file? Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-377601 Share on other sites More sharing options...
hotrod57 Posted October 25, 2007 Author Share Posted October 25, 2007 Here is my text file: title = Test Form; checkbox = shoe; checkbox = hair; radio_button = 3; comment_box dropbox = 18-29; dropbox = 30-49; dropbox = 50-75; textbox = 50; Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-377757 Share on other sites More sharing options...
Wuhtzu Posted October 25, 2007 Share Posted October 25, 2007 And what exactly shall: title = Test Form; checkbox = shoe; checkbox = hair; radio_button = 3; comment_box dropbox = 18-29; dropbox = 30-49; dropbox = 50-75; textbox = 50; Please post the html form which you wanna produce from the above text. Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-378016 Share on other sites More sharing options...
Psycho Posted October 25, 2007 Share Posted October 25, 2007 Ok, what you stated in your first post is not acurate based upon the data you are using. You will not get $array[0] = checkbox, checkbox, radio button $array[1] = shoe, hat, car. from that data because the array is getting redefined on each itteration of the loop. So the first time through you would have $array[0]='checkbox' & $array[1]='shoe'. On the 2nd iteration it would be $array[0]='checkbox' & $array[1]='hat', etc. Try this: <?php $lines = file('test_read.txt'); foreach ($lines as $line) { list($field, $value) = explode("=", $line); $value = substr(trim($values), 0, 1); switch(trim($field)) { case 'checkbox': print ("<input type=\"checkbox\" name=\"userbox\" value=\"$value\">$value"); break; case 'radio': print ("<input type=\"radio\" name=\"userbox\" value=\"$value\">$value"); break; } } ?> however, I don't know how that is going to work for you since all the field names will be the same. Hope you have some logic to handle that. Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-378066 Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 Using mjdomato's code provided (which is very nice) here is an alternative: <?php $lines = file('test_read.txt'); foreach ($lines as $line) { list($field, $value) = explode("=", $line); $value = substr(trim($values), 0, 1); echo get_html($field, $value); } function get_html($tag, $value) { switch(trim($tag)) { case 'checkbox': return "<input type=\"checkbox\" name=\"userbox\" value=\"$value\">$value"; break; case 'radio': return "<input type=\"radio\" name=\"userbox\" value=\"$value\">$value"; break; // more cases below if needed. } } ?> That way its more versatile returning it because you can then use this in other sites and keep that code in a functions file. Just another way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-378069 Share on other sites More sharing options...
hotrod57 Posted October 25, 2007 Author Share Posted October 25, 2007 Tried that and this is what I get: checkbox checkbox radio Those words are the symbols for what I get, two checkboxes and a radio button, with no values. Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-378247 Share on other sites More sharing options...
Psycho Posted October 26, 2007 Share Posted October 26, 2007 My sig does state there may be typos! Change this $value = substr(trim($values), 0, 1); To this $value = substr(trim($value), 0, 1); Quote Link to comment https://forums.phpfreaks.com/topic/74659-solved-reading-from-an-array/#findComment-378363 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.