Jump to content

print class variable high up the page


isedeasy

Recommended Posts

I have a class that I created, halfway down my page I call a function from the class which prints some code and also changes a variable inside the class.

 

It's a search class basically it prints out the search results and saves the total amount of results into the variable.

 

I want to display the amount of results above the results like so:-

 

echo $Search->results;

$Search->GetResults('foo');

 

The problem is that $Search->results will be equal to NULL until the variable is updated in the function GetResults.

 

Hope that makes sense, am I going about this wrong?

Link to comment
Share on other sites

why not just print $this->results inside your getResults function before it prints the other stuff? Alternatively, you could have your getResults function return a string with the output instead of printing it. This is probably the better option. Then you can do

$str = $Search->GetResults('foo');
$res = $Search->results;

 

and use these variables whenever you need, instead of being forced to use the output when you call them.

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.