1

I am making a module displaying google map and retrieving the data in the marker info box from database. It is a hotel booking website. The currency stored in the database is Hotel's default currency while my goal is to show currency according to user's location.

I am using geoplugin class but it stops working when I try to pass variable or try to set variable in the class. I fetched the currency code from the database and tried to pass as variable to set it as base currency but it didn't work.

$curr = "USD"; // say "USD" is fetched from database

class geoPlugin {

  public $curr;
  //the geoPlugin server
  var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';


  //the default base currency
  var $currency = $curr;
  or $currency = $this->curr;

I also tried to set the variable inside the class

class geoPlugin {

  var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';
  public $curren;

  public function setVariableOne($curr) {
  $this->$curren = $curr;
}

//the default base currency
var $currency = $curren;
and in index file

$geoplugin = new geoPlugin();
$geoplugin->setVariableOne('USD');
2
  • first, did you get any error? Commented May 24, 2015 at 12:01
  • Yes there was an error. Class is not accepting variable value for $currency.
    – Agha
    Commented May 24, 2015 at 12:38

1 Answer 1

2

First of all change

$this->$curren = $curr;

To

$this->curren = $curr;

And add default to property just after class opening tag (not in method)

public $curr = 'USD';

Regards

EDIT1 :

class geoPlugin {

    public $curren;

    public function setVariableOne($curr) 
    {
        $this->curren = $curr;
    }
}

thats work try if this code not work for you plz give us some hints by using errrors ; )

edit2:

do this and tell me if is working for you ( save it to new file ).

class geoPlugin {

    public $curren = 'USD';

    public function setVariableOne($curr) 
    {
        $this->curren = $curr;
    }

    public function getVariableOne()
    {
        return $this->curren;
    }
}

$geo = new geoPlugin();
var_dump( $geo->getVariableOne() ); // return 'USD' string
$geo->setsetVariableOne('EUR');
var_dump( $geo->getVariableOne() ); // return 'EUR' string

if this give you a blank page, please set error_reporting(-1) and then tell me the error code.

pastebin with geoplugin.class.php and it's working as it should be: http://pastebin.com/FWkYVsjg

6
  • Thanks for indicating the typo. My problem is, I need to pass the default currency in variable as it is coming from database. If I try to pass a variable in $currency, the code stops working. I also tried to set a variable just after class opening tag and pass to $currency.
    – Agha
    Commented May 24, 2015 at 12:32
  • I did the same. The screen goes blank whenever I try to pass a variable to $currency (like: var $currency = $curren; )
    – Agha
    Commented May 25, 2015 at 5:31
  • Same. When I tried to pass var $currency = $curren; , I got following error. Parse error: syntax error, unexpected T_VARIABLE in .....
    – Agha
    Commented May 25, 2015 at 7:45
  • you can not do var $currency = $curren in class out of the method. Just paste my edit into your new php file and look what i saying about.
    – Lanah
    Commented May 25, 2015 at 17:54
  • I have tried your code but the class is taking USD as default in any case. Please find the code here link
    – Agha
    Commented May 26, 2015 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.