harchew Posted September 6, 2007 Share Posted September 6, 2007 I created a function name createTextField() to display all my textfield. function createTextField($name,$value) { return "<input type=\"text\" name=\"$name\" value=\"$value\" class=\"txtField\">"; } I declared the function with a variable : $input_addcat = createTextField('addcat',NULL); and display it using echo $input_addcat. its works perfectly for this case. However when I tired using the same function for drop list : function createList() { return "<select name=\"a\"> for ($a=0; $a < 10; $a++){ <option value=\"a\">1</option> } </select>"; } decalre it by $list = createList(); and display it using echo $list; The drop list only show a single value. For some reason the for loop didn't work in the function. Any solution to it? I have been working all night long just to figure out a solution for it. Really appreciate your help. Thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/68276-solved-call-drop-list-from-a-function/ Share on other sites More sharing options...
Jessica Posted September 6, 2007 Share Posted September 6, 2007 because you're returning a string which has the words for($a...etc in it. Not php code. You need to use the for loop to append strings to the returning string. Quote Link to comment https://forums.phpfreaks.com/topic/68276-solved-call-drop-list-from-a-function/#findComment-343281 Share on other sites More sharing options...
harchew Posted September 6, 2007 Author Share Posted September 6, 2007 because you're returning a string which has the words for($a...etc in it. Not php code. You need to use the for loop to append strings to the returning string. Thanks for the quick reply. I understand what you mean, I tried various way including declare it as a variable instead of string but still can't work. Perhaps you can show me an example. Thank U.... Quote Link to comment https://forums.phpfreaks.com/topic/68276-solved-call-drop-list-from-a-function/#findComment-343294 Share on other sites More sharing options...
Jessica Posted September 6, 2007 Share Posted September 6, 2007 <?php $str = '<select name="a">'; for ($a=0; $a < 10; $a++){ $str .= '<option value="a">1</option>'; } $str .= '</select>'; return $str; ?> By the way, all 10 options will be identical according to your code. Quote Link to comment https://forums.phpfreaks.com/topic/68276-solved-call-drop-list-from-a-function/#findComment-343309 Share on other sites More sharing options...
harchew Posted September 7, 2007 Author Share Posted September 7, 2007 Wow it's works. Really thanks for the solution. Learned something new today... Quote Link to comment https://forums.phpfreaks.com/topic/68276-solved-call-drop-list-from-a-function/#findComment-343685 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.