Branden Wagner Posted December 5, 2007 Share Posted December 5, 2007 for($i = 1; $i == $school_num; $i++) { ${school_$i_tmp} = strip_tags(strip_mq_gpc($_POST['school_${i}'])); ${"school_$i} = preg_replace('/\r?\n/', '<br>', ${school_$i_tmp}); } whats wrong with this? how can i fix it? thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 5, 2007 Share Posted December 5, 2007 What are you trying to do? What do you expect this code to do? What is it doing? Ken Quote Link to comment Share on other sites More sharing options...
Branden Wagner Posted December 5, 2007 Author Share Posted December 5, 2007 Sorry, i guess i should have put that in the first post.. when the loop is done it should have variables like $school_1_tmp $school_1 $school_2_tmp $school_2 ... based on the variable $school_num they sey earlier on in the form(usually ranges between 1 and 5). Basically i was trying to create a loop to iterate through the users input, to collect information on the schools they input. i didnt know how to put the 2 lines on into 1 thus why i used school_1_tmp and school_1... so if its a better idea to combine them, that works for me too. right now i get syntax errors... Unexpected T-variable. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2007 Share Posted December 5, 2007 When you have a varying length array of data, you should use an array to hold it. This will make your code simpler and general purpose so that it will work with any number of values. By using sequentially numbered variables, you need to carry around another variable to keep track of how many of them there are. With an array you don't need to do that, just use the count() function if you should ever need to know how many or even simpler, just iterate over them directly using a foreach() loop. Quote Link to comment Share on other sites More sharing options...
Branden Wagner Posted December 5, 2007 Author Share Posted December 5, 2007 Well, on the webpage, there is a drop down box How Many School are you going to put information for: [Dropdown] {Javascript shows multiline text boxes for each school based on the above dropdown} so then how would i process that using php in an array? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2007 Share Posted December 5, 2007 The normal way would be if the javascript/form generates the input fields with names - "school[]" (and let the index be generated by the position on the form) or explicitly specify the index in the name - "school[0]", "school[1]",... Both of those methods will result in an array - $_POST['school'][0], $_POST['school'][1]... Assuming that you want the values in $school[0], $school[1]..., (if you want the indexes to be 1, 2, 3,... instead of 0,1,2... add the $key=> construct to the foeach() loop and use $school[$key+1] instead of $school[] in the loop) the equivalent foreach loop for the code you posted would look like - foreach($_POST['school'] as $value) { $school[] = preg_replace('/\r?\n/', '<br>', strip_tags(strip_mq_gpc($value))); } Quote Link to comment 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.