json - PHP constructor function single param -


update: getting php error: trying property of non-object on line: if ($card->name == $fileref){

i'm constructing object in php using __construct(x='') { definition } , calling function $var = new object('string');

the constructor receives string ie 'file' related corresponding 'file.php'. there catalogue of available file.php class in json file, contains directory information.

and catching error, perhaps syntax? ... got me 2 days now, want take swing?

public function __construct($ctitle = '') {     $fileref = $ctitle.'php';      //get json card directory     $this->carddirlocation = 'thisismyserver.com/carddir.json';     $this->carddir = file_get_contents($this->carddirlocation);     $this->cardarray = json_decode($this->carddir, true);       //find card listing carddir.json based on form response input , construct card class instance or use main page (default)     foreach ($this->cardarray['cards'] $key => $val) { //search through each card err??         if ($card->name == $fileref){ //if name matches name in carddir.json file             //fill values         }                     if ($this->title == ''){ //or if there no title             $this->title = 'get started @ home page'; //refer default values -> home page             $this->dir = 'cards/start/';             $this->name = 'start.php'; ...etc  

the err: can't $fileref variable match in json file array, goes 'else if' default. json file looks this:

{"cards":[{"title":"something", "name":"file.php", "dir":"somefile/dir/here/" }, {"title":"different something", "name":"xfiles.php", "dir":"somefile/dir/there/" ...etc

you might want loop through entire array before returning or setting catchall case. here how so:

foreach ($this->cardarray['cards'] $card) {     if ($card->name == $fileref) {     // success actions here      return true;     } }  // failure actions here. reached if foreach loop found nothing. 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -