Jump to content

Experts Please Help - Setting a Variable Within a Class


webref.eu

Recommended Posts

I have a class which defines the $sQuery variable as per the below:

 

class shopcore
{
var $iMerchantId;
var $iCategoryId	= 0 ;
var $iProductId;
var $sQuery;

 

later in the class, sQuery gets set as follows: 

 

$this->sQuery 		= strlen($_GET['q']) > 0 				? trim( strip_tags( urldecode($_GET['q']) ) ) 	: null;

 

This whole class gets included in a file, let's call it sony-laptop.php.  Thing is, I want to manually set $sQuery to have a value of "sony laptop".  I don't want to use the querystring q to set this, I just want to manually define another variable in my sony-laptop.php file, lets called it $KeyphrasePage, and then set $sQuery to this. 

 

I've tried stuff like this: 

 

Early in sony-laptop.php file, define the first variable:

 

$KeyphrasePage = "sony laptop";

 

Then within class try and set sQuery to this:

 

$this->sQuery = $KeyphrasePage;

 

but it's not getting set at all, it seems $KeyphrasePage isn't passed into the class.  :'( So how to I pass my $KeyphrasePage into the class properly and then set sQuery to it???

 

Any help much appreciated.

 

Thanks

In your shopCore class file, put the following:

 

    function set_sQuery($yourVariable){

            $this->sQuery= $yourVariable;

        }

 

Then, in sony-laptop.php you can set the value by:

$KeyphrasePage = 'sony laptop';

$shopCore->set_sQuery($KeyphrasePage);

Thanks for your reply.  I still haven't quite got it working. 

 

The class, which is being included in the master file, sony-laptop.php, is:

 

class shopcore
{
var $sQuery;

 

Then within this is the function: 

 

public function gatherEnvData()

 

Which contains:

 

if ( strlen($_GET['q']) > 0 ) {
	$this->sQuery 		= strlen($_GET['q']) > 0 				? trim( strip_tags( urldecode($_GET['q']) ) ) 	: null; 
	}
	else {
	$this->sQuery = $KeyphrasePage;

 

I'm still not managing to set sQuery, have I got the lines in the original file wrong?  In original file I have:

 

$KeyphrasePage = "sony";
$gatherEnvData->set_sQuery($KeyphrasePage);

 

 

Thanks

 

 

 

 

 

Your code doesn't make sense to me.

 

I think urldencode should be urlencode though.

 

Edit: my mistake, you're right. urldencode is correct (the opposite of urlencode!!).

 

Do you have the set_sQuery method defined? Also, are you sure sQuery isn't an empty value (rather than not being set)?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.