Jump to content

Recommended Posts

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 ;D

 

$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

<?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?

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();
?>

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.

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

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.

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 =)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.