-1

更新:我收到这个 php 错误:尝试在这一行获取非对象的属性:if ($card->name == $fileRef){

我正在使用__construct(x='') { definition }和调用函数在 php 中构造一个对象$var = new Object('string');

构造函数接收与相应的“file.php”相关的字符串,即“file”。json 文件中包含该类的所有可用 file.php 的目录,其中包含目录信息。

我发现了一个错误,也许是我的语法?... 两天了,想去荡秋千吗?

public function __construct($ctitle = '')
{
    $fileRef = $ctitle.'php';

    //Get the json card directory
    $this->cardDirLocation = 'thisismyserver.com/CardDir.json';
    $this->cardDir = file_get_contents($this->cardDirLocation);
    $this->cardArray = json_decode($this->cardDir, true);


    //Find the card listing from CardDir.json based on form response input and construct a Card class instance or use the main page (default)
    foreach ($this->cardArray['Cards'] as $key => $val) { //search through each card IS THIS MY ERR??
        if ($card->name == $fileRef){ //if the name matches a name in the cardDir.json file
            //Fill Values
        }
                    if ($this->title == ''){ //or if there is no title
            $this->title = 'Get Started at The Home Page'; //refer to default values -> the home page
            $this->dir = 'cards/start/';
            $this->name = 'start.php'; ...etc 

错误:我无法让 $fileRef 变量与 json 文件数组中的任何内容相匹配,因此它总是进入“else if”默认值。json 文件看起来像这样:

{"Cards":[{"title":"Something", "name":"file.php", "dir":"somefile/dir/here/" }, {"title":"不同的东西", "name":"xfiles.php", "dir":"somefile/dir/there/" ...等

4

1 回答 1

0

在返回或设置一个包罗万象的情况之前,您可能希望遍历整个数组。以下是如何执行此操作:

foreach ($this->cardArray['Cards'] as $card) {
    if ($card->name == $fileRef) {
    // Your success actions here
     return true;
    }
}
 // Your failure actions here. This will only be reached if the foreach loop found nothing.
于 2013-05-14T17:28:05.677 回答