happs Posted October 15, 2007 Share Posted October 15, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/73324-solved-returning-an-array-from-a-function/ Share on other sites More sharing options...
wildteen88 Posted October 15, 2007 Share Posted October 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/73324-solved-returning-an-array-from-a-function/#findComment-370050 Share on other sites More sharing options...
happs Posted October 16, 2007 Author Share Posted October 16, 2007 Hi, Thanks for that. Yeh it works now... just trying to work out WHY it works. Heh. - Alex Quote Link to comment https://forums.phpfreaks.com/topic/73324-solved-returning-an-array-from-a-function/#findComment-370613 Share on other sites More sharing options...
wildteen88 Posted October 16, 2007 Share Posted October 16, 2007 Hi, Thanks for that. Yeh it works now... just trying to work out WHY it works. Heh. - Alex Did I not explain that above? Quote Link to comment https://forums.phpfreaks.com/topic/73324-solved-returning-an-array-from-a-function/#findComment-370870 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.