Jump to content

[SOLVED] Returning an array from a function


happs

Recommended Posts

Hey people :)

 

I'm slowly but surely getting there ..

 

This script is to check an input field that asks for a name of a pool league.

It is meant to check for leagues with the same name, and if one is found, show the name of the duplicate league and then give the option to edit it - instead of creating a duplicate. If one of the same name isn't found then check_duplicate returns FALSE and the new league is added.

 

HOWEVER ...

 

It does't work. Hehe.

I get the following errors ..

 

Notice: Undefined variable: row_leagues in C:\Websites\maccpoolleague\leagues_ex.php on line 159

Notice: Undefined variable: row_leagues in C:\Websites\maccpoolleague\leagues_ex.php on line 159

Notice: Undefined variable: row_leagues in C:\Websites\maccpoolleague\leagues_ex.php on line 160

 

Here's the function:

 

function check_duplicate(){
   $query_leagues = mysql_query("SELECT id AS id, name AS name, format AS format FROM leagues"); // open connection.
   if (mysql_num_rows($query_leagues) > 0) { // only run the check if there is one or more leagues.
      while ($row_leagues = mysql_fetch_array($query_leagues)) { // start streaming the leagues.
         if (strtoupper(trim($_POST['name'])) == $row_leagues['name'] && $_POST['format'] == $row_leagues['format']) { // look for a match of league names and format.
            return ($row_leagues); // if found, finish function and return array - $row_leagues ['name'], ['id'] and ['format']
         }
      }
      return false; // if, after streaming the leagues, none are found that match the input then return false.
   } else {
      return false; // ditto for no leagues already in database, as it doesn't need to check.
   }
}

 

And here's the function call:

 

         if (check_duplicate()) { // is it TRUE, uh OH ! YES! Better make them aware!
            echo '<b>Error:</b> There is already a league with that name and format: <br />' . $row_leagues['name'] . ' (' . $row_leagues['format'] . ')';
            echo ' [ <a href="' . $_SERVER['PHP_SELF'] . '?action=edit&league_id=' . $row_leagues['id'] . '">EDIT THIS LEAGUE</a> ]';
         } else { // It's FALSE then so add it.
            add_league();
         };

 

Thanks for your help in advance :)

 

- Alex

Link to comment
Share on other sites

When you return a variable from a function, it will not mean that you can use that function outside of the function. Instead what will happen is PHP will return the value of that variable.  In order to catch to the returned value you will need to assign your function to variable, eg:

$myvar = check_duplicate();

$myvar will hold the value returned from the that function.

 

So in order to use $row_league outside of the function change this line:

if (check_duplicate()) { // is it TRUE, uh OH ! YES! Better make them aware!

 

To:

if ($row_leagues = check_duplicate()) { // is it TRUE, uh OH ! YES! Better make them aware!

 

Now you should be able to use $row_leagues outside of the function.

 

Link to comment
Share on other sites

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.