portabletelly Posted May 15, 2007 Share Posted May 15, 2007 Hi I was wondering if it was possible to turn this bit of code into a function? I use it many times accross several different pages. Basically the code is a dynamic select list which goes into a html form. What i was thinking was to turn it into a function and put it on a page by itself called select_customers.php then use include("select_customers.php") on the pages I requried it. Is this possible? If so how would I turn this into a fuction What syntax would I need to include the funtion on a seperate page. Thanx in advanced $query = "SELECT name FROM customer_tb"; $result = mysql_query($query, $link); if(mysql_num_rows($result)) { // we have at least one user, so show all users as options in select form while($row = mysql_fetch_row($result)) { print("<option value=\"$row[0]\">$row[0]</option>"); //$test="while"; } } else { //$test="else"; print("<option value=\"\">No users created yet</option>"); } Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/ Share on other sites More sharing options...
AbydosGater Posted May 15, 2007 Share Posted May 15, 2007 <?php function createList(){ $query = "SELECT name FROM customer_tb"; $result = mysql_query($query, $link); if(mysql_num_rows($result)) { // we have at least one user, so show all users as options in select form while($row = mysql_fetch_row($result)) { print("<option value=\"$row[0]\">$row[0]</option>"); //$test="while"; } } else { //$test="else"; print("<option value=\"\">No users created yet</option>"); } } // End Function ?> Then just include the file at the top of every page. And in the file where you want the code to run just add <? createList(); ?> and it will run the code. Hope this helps. Andy Edit: You have the title as Functions within functions, is that code already within a function? Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253896 Share on other sites More sharing options...
portabletelly Posted May 15, 2007 Author Share Posted May 15, 2007 I thought mysql_query($query, $link); was a function. Just wasnt sure how php would handle queryies within a funciton. Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253900 Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 function returning the required code. try <?php function customer_select() { $res = '<select name="customer">'; $query = "SELECT name FROM customer_tb"; $result = mysql_query($query); if(mysql_num_rows($result)) { // we have at least one user, so show all users as options in select form while($row = mysql_fetch_row($result)) { $res .= "<option value=\"$row[0]\">$row[0]</option>"; //$test="while"; } } else { //$test="else"; $res .= "<option value=\"\">No users created yet</option>"; } $res .= '</select>'; return $res; } echo customer_select(); ?> Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253903 Share on other sites More sharing options...
AbydosGater Posted May 15, 2007 Share Posted May 15, 2007 Yes it is a function! PHP allows functions to be used within other functions. I dont think you can create functions inside others IE: But ive never had to try so i dont know. function number1(){ function number2(){ //im inside another } } That code i posted before should work fine. EDIT: Yeah sorry thanks Barand, I didnt see the return. If you going to run the code you must use <? echo customer_select(); ?> not just customer_select() sorry bout that. Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253904 Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 You can <?php function foo($n) { function bar($x) { return $x * 10; } return bar($n); } echo foo(1); // --> 10 ?> but a couple of things to be careful of -- If you call bar() before you call foo() you get "Undefined function bar()" error -- If you call foo() a second time you get "bar() already defined" error Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253918 Share on other sites More sharing options...
AbydosGater Posted May 15, 2007 Share Posted May 15, 2007 Ah, ok.. Well i dont think many people would be needing to do it. If it was absolutely needed im sure a class could be used. Andy Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253923 Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 A comment on mysql_query() inside a function. If you use the optional $link argument then you need either to declare $link as global or pass it to the function. Easier to omit it and the last connection is used by default. Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253926 Share on other sites More sharing options...
per1os Posted May 15, 2007 Share Posted May 15, 2007 A note on the function inside function. To defeate the error message of function already defined implement it like this =) <?php function foo($n) { if (!function_exists('bar')) { function bar($x) { return $x * 10; } } return bar($n); } echo foo(1); // --> 10 ?> Basically it only defines the function once and does not break the code =) Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253936 Share on other sites More sharing options...
Barand Posted May 15, 2007 Share Posted May 15, 2007 I think it's a weakness of the language that you have to do that. If you declare a function within a function, then, like a variable declared inside a function, it should only be available within, and for the duration of, the outer function. ie. Function scope, in this case, should be the same as variable scope. Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-253963 Share on other sites More sharing options...
AbydosGater Posted May 16, 2007 Share Posted May 16, 2007 Yes, I Agree. Maybe we will see this in a newer version, Would be helpful to many. Andy Link to comment https://forums.phpfreaks.com/topic/51558-functions-within-functions/#findComment-254282 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.