PHP对象和实例

PHP对象和实例,php,arrays,oop,instance-variables,Php,Arrays,Oop,Instance Variables,我将尽可能简单地分解它,然后发布所有必要的代码 目标:要获得所有用户在一个时段内每个子时段的所有答案,以及没有答案的答案,我将标记单元格(不必担心这部分),以便稍后对其执行数据外推 用户类:用户具有Id和句点列表 期间类:期间具有Id和子期间列表 子时段类:子时段具有Id和问题列表 问题类:每个问题都有答案 现在,代码、前两个嵌套循环中的注释行是首先弄清楚的关键。每个例子中的第二行是我迄今为止的尝试。在第二行都未注释的情况下,我在这一行得到一个错误: $user->periods[$period

我将尽可能简单地分解它,然后发布所有必要的代码

目标:要获得所有用户在一个时段内每个子时段的所有答案,以及没有答案的答案,我将标记单元格(不必担心这部分),以便稍后对其执行数据外推

用户类:用户具有Id和句点列表

期间类:期间具有Id和子期间列表

子时段类:子时段具有Id和问题列表

问题类:每个问题都有答案

现在,代码、前两个嵌套循环中的注释行是首先弄清楚的关键。每个例子中的第二行是我迄今为止的尝试。在第二行都未注释的情况下,我在这一行得到一个错误:
$user->periods[$period]->addSubPeriod($period->periodId,$subPeriod)

说: 注意:未定义的偏移量:1致命错误:对非对象调用成员函数addSubPeriod()

$periods_arr = array(1, 2, 3, 4, 5);
$subPeriods_arr = array(1, 2);
$questionslist = array("q_1_1", "q_1_2", "q_2_1", "q_2_2", "q_3_1", "q_4_1", "q_5_1");

class User {
    public $userId;
    public $periods = array();

    public function __construct($number) {
        $this->userId = $number;
    }

    public function addPeriod($id, $pno) 
    {
        $this->periods[$id] = new Period($pno);
    }
}

class Period {
    public $periodId;
    public $subPeriods = array();

    public function __construct($number)
    {
        $this->periodId = $number;
    }

    public function addSubPeriod($id, $spno)
    {
        $this->subPeriods[$id] = new SubPeriod($spno);
    }
}

class SubPeriod {
    public $subPeriodId;
    public $answers = array();

    public function __construct($number)
    {
        $this->subPeriodId = $number;
    }

    public function addAnswer($id, $answer)
    {
        $this->answers[$id] = new Question($answer);
    }
}

class Question {
    public $answer;

    public function __construct($ans)
    {
        $this->answer = $ans;
    }

    public function getAnswer()
    {
        echo $this->answer; 
    }
}        

$userlist = array();

$sql = 'SELECT user_ref FROM _survey_1_as GROUP BY user_ref ORDER BY user_ref ASC';
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))
{
    array_push($userlist, new User($row['user_ref']));
}

foreach ($userlist as &$user)
{
    foreach ($periods_arr as &$period)
    {
        // set the current users ($user) period
        // $user->addPeriod($user->userId, $period);

        foreach ($subPeriods_arr as &$subPeriod)
        {
            // set the sub_period for the current users period
            // $user->periods[$period]->addSubPeriod($period->periodId, $subPeriod);

            foreach($questionslist as &$aquestion)
            {
                $sql = 'SELECT ' . $aquestion . ' FROM _survey_1_as WHERE user_ref = ' .
                    $user->userId . ' AND answer_period = ' . $period . ' AND answer_sub_period = ' . 
                    $subPeriod .''; 

                $result = mysql_query($sql);

                while ($row = mysql_fetch_array($result))
                {
                    $user->periods[$period]->subPeriods[$subPeriod]->addAnswer($row[$aquestion]);       
                }
            }
        }
    }   
}

错误信息很多。您的数组没有索引为1的元素(未定义的偏移量:1),因此尝试使用它与尝试执行
$x=null$x->doSomething(),这当然会导致致命错误:调用成员函数。。。在非对象上


你可能想
var\u dump($user->periods)
仔细调试你的代码。

等3分钟,由于某种原因,我现在的代码一点也没有出错,但我还有最后一个问题。我将编辑原始代码/帖子,然后我将在此处进行评论,同时停止使用
&
进行迭代,这可能会导致奇怪的问题,就我而言,您在您的场景中根本不需要它。是的,我最近看到了&符号。不管怎样,我已经弄明白了,而且效果很好。我将在8小时后发布答案,因为我还不能发布。已经整理好的将很快发布答案。在过去的帖子中,每个人都提出了一个简单的问题——复杂的LOL