Jump to content

Recommended Posts

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

}

}

 

Link to comment
https://forums.phpfreaks.com/topic/191522-returning-values-from-functions/
Share on other sites

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

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

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.