chomedey Posted February 9, 2010 Share Posted February 9, 2010 Hi all, I am calling a function from a script, and wanting that function to return a variable back to the script so I can then use it in another function called by the script. Although I can echo the variable at the end of the function, when I try and return it to the original script, using "return", I get an error message - unrecognized variable. Any ideas? I've been trying different things for hours. Below is the code (I'm using BBEdit but I don't know how to retain the styled text in this text box): This is the original script: if ((isset($_POST['submitted']) ) && (!empty($_POST['category']) )) { // Handle the form $category = ($_POST['category']); $limit_number = 0; $dbc = mysql_connect('localhost', 'root', 'One4Two'); mysql_select_db('mds'); $query = "SELECT * FROM deepshares WHERE category = '$category' ORDER BY date_entered DESC LIMIT $limit_number,11"; display_results($query); display_prev_next('categories', $limit_number, $num_results, $category); } And this is the function: function display_results($query) { if ($r = mysql_query($query)) { while ($row = mysql_fetch_array($r)) { $entry = $row['entry']; $id = $row['entryID']; $entry_length = str_word_count($entry); $num_results = mysql_num_rows($r); if ($entry_length>=20) { echo "<p><h3>Title: {$row['title']}</h3> Author: {$row['username']}<br /> Date submitted: {$row['date_entered']}<br /> Views: {$row['views']}<br /> Category: {$row['category']}<br />"; echo substr($entry, 0,100); echo "... <a href='view_entry.php?id=".($id)."'>(more)</a><br />"; } else { echo "<p><h3>Title: {$row['title']}</h3> Author: {$row['username']}<br /> Date submitted: {$row['date_entered']}<br /> Views: {$row['views']}<br /> Category: {$row['category']}<br />"; echo $entry; } } } else { print '<p>Could not retrieve the data because:<br />' . mysql_error(). '.</p> <p>The query being run was: ' . $query . '</p>'; } return $num_results; } And here is the function I want to call once the variable $num_results has been returned: function display_prev_next($link_page, $limit_number, $num_results, $category) { if (($limit_number >= 10) && ($num_results > 10)) { echo "<p><a href='".($link_page).".php?ln=".($limit_number - 10)."&category=".($category)."'>Prev</a>"; echo " "; echo "<a href='".($link_page).".php?ln=".($limit_number + 10)."&category=".($category)."'> Next</a></p>"; } else if (($limit_number >= 10) && ($num_results <=10)) { echo "<p><a href='".($link_page).".php?ln=".($limit_number - 10)."&category=".($category)."'>Prev</a>"; echo " "; } else if (($limit_number <= 10) && ($num_results <=10)) { echo ""; } else { echo "<p><a href='".($link_page).".php?ln=".($limit_number + 10)."&category=".($category)."'>Next</a></p>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/191522-returning-values-from-functions/ Share on other sites More sharing options...
Fergal Andrews Posted February 9, 2010 Share Posted February 9, 2010 Hi chomedy, You need to assign a variable to the function call in your script. At the moment there is nothing in your script to catch the returned value. Try changing display_results($query); to $num_results = display_results($query); Alternatively you can make $num_results global, but unless you intend using that value somewhere else, I'd just do it as above. I also noticed that the conditional statement at the start of display_results is incorrect. At moment it is setting the value of $r and not checking it. if ($r = mysql_query($query)) { should be if ($r == mysql_query($query)) { I haven't had time to check your script, so I hope that works. Fergal Quote Link to comment https://forums.phpfreaks.com/topic/191522-returning-values-from-functions/#findComment-1009618 Share on other sites More sharing options...
wildteen88 Posted February 9, 2010 Share Posted February 9, 2010 return does not return the variable itselft. It only return the variables value. You'll need to capture the return value with a variable. For example $results = display_results($query); For more information go to http://php.net/manual/en/functions.returning-values.php Quote Link to comment https://forums.phpfreaks.com/topic/191522-returning-values-from-functions/#findComment-1009621 Share on other sites More sharing options...
chomedey Posted February 9, 2010 Author Share Posted February 9, 2010 Once again awesome help from php freaks. Thanks so much. Julian Quote Link to comment https://forums.phpfreaks.com/topic/191522-returning-values-from-functions/#findComment-1009628 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.