0

I am getting undefined variable for periods and subPeriods on the last line of this program. not sure what the problem is. Could it be my instances?

This is my first proper attempt at oop in PHP so i am sure i am doing something wrong.

$global_periods = 5;
$global_subperiods = 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($pno)
    {
        $periods[] = new Period($pno);
    }
}

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

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

    public function addSubPeriod($spno)
    {
        $subPeriods[] = new SubPeriod($spno);
    }
}

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

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

    public function addAnswer($answer)
    {
        $answers[] = new Answer($answer);
    }
}

class Question {
    public $answer;

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

    public function getAnswer()
    {
        echo $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))
{
    $userlist[] = new User($row['user_ref']);
}

for ($i = 0; $i >= count($userlist); $i++)
{
    for ($x = 1; $x > $global_periods; $x++)
    {
        $userlist[i]->addPeriod($x);

        for ($y = 1; $y > $global_subperiods; $y++)
        {
            $userlist[i]->$periods[x]->addSubPeriod($y);

            foreach($questionslist as $aquestion)
            {
                $sql = 'SELECT ' . $questionNumber . ' FROM _survey_1_as WHERE user_ref = ' .
                     $i . ' AND answer_sub_period = ' . $y . ' AND answer_period = ' . $x .''; 

                $result = mysql_query($sql);

                while ($row = mysql_fetch_array($result))
                {
                    $userlist[i]->$periods[x]->$subPeriods[y]->addAnswer($row[$questionNumber]);
                }
            }
        }
    }   
}

$userlist[3]->$periods[2]->$subPeriods[2]->getAnswer();
8
  • 1
    takeout all $ after $userlist[3]-> Commented May 27, 2014 at 7:24
  • 2
    You have to dimiss the $ sign for fields in objects. $userlist[3]->periods[2]->subPeriods[2]->getAnswer(); Commented May 27, 2014 at 7:26
  • 1
    Notice: Undefined offset: 2 in C:\xampp\htdocs\Site\data_portal\maths.php on line 125 Notice: Trying to get property of non-object in C:\xampp\htdocs\Site\data_portal\maths.php on line 125 Fatal error: Call to a member function getAnswer() on a non-object in C:\xampp\htdocs\Site\data_portal\maths.php on line 125 Line 125 is the last line. got those errors are making your changes. Commented May 27, 2014 at 7:26
  • You are inserting a Period with $x=1 into periods[0]. When you try to access that Period by using $x as index, you try to get the wrong item. Obviously, an undefined one. Same for subPeriods Commented May 27, 2014 at 7:29
  • There are 5 periods and 2 sub periods so i changed them to this... for ($y = 0; $y > 1; $y++) and for ($x = 0; $x > 5; $x++) but still getting errors I think its with the answers now Commented May 27, 2014 at 7:32

1 Answer 1

1

Remove all the $ signs behind the $userlist, you only need to define the first variable. You can't use dollar signs like this, this way, it will try get the value of the word after the $ sign and call that, but that variable doesn't exist.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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