0

I do not understand fully why this PHP code is not saving the data in the arrays. when I echo out the array during the while loop, the values are set but after I exit the loop, the values in the array are missing. The other variables did contain their values after leaving the array.

<?php
    class VIEWARRAY {
        public $year;
        public $month;
        public $day = array();
        public $views = array();
    }

    $viewdatabase = array();

    $connection = MySQL_Connection($host, Session_Get("username"), Session_Get("password"), $database);

    $dayviewindex = 0; $monthyearindex = 0; $previousmonth = "";
    $information = MySQL_Script($connection, "SELECT * FROM viewdatabase");
    while ($index = mysqli_fetch_array($information)) {
        if ($index["month"] != $previousmonth) {    
        $viewdatabase[$monthyearindex] = new VIEWARRAY;
        $viewdatabase[$monthyearindex]->year = $index["year"];
        $viewdatabase[$monthyearindex]->month = $index["month"];

            $dayviewindex = 0;
        $monthyearindex++;
        $previousmonth = $index["month"];
        }

        $viewdatabase[$monthyearindex]->day[$dayviewindex] = $index["day"];
        $viewdatabase[$monthyearindex]->views[$dayviewindex] = $index["views"];
        $dayviewindex++;
    }

    MySQL_Disconnect($connection);

    //testing area
    echo "->" . $viewdatabase[0]->year . " + " . $viewdatabase[0]->month . "<br />"; //prints out ->2013 + 8
    echo "------>" . $viewdatabase[0]->day[2] . " + " . $viewdatabase[0]->views[2] . "<br />"; //prints out ------> + 
    echo count($viewdatabase[0]->views) . "<br />"; //prints out 0
    echo count($viewdatabase[1]->views) . "<br />"; //prints out 0

    ?>

I made sure that I was able to connect to my database just fine and that my database did return information. This is how my database is setup

year     month     day     views
2013     8         25      1
2013     8         26      1
2013     8         27      1
2013     9         3       1
0

2 Answers 2

0

At first run, after if ($index["month"] != $previousmonth), $monthyearindex will be 1. That means you'll next try to access $viewdatabase[1] but it's NULL. On the next iteration you won't enter the if, $monthyearindex will be 1, and $viewdatabase[1] will still be NULL. So you never actually get to set anything in day and views.

1
  • Thank you for your quick response. I moved the $monthyearindex up to the top and set the initial $monthyearindex to -1. That solved the problem. Commented Sep 6, 2013 at 23:49
0

Use

if ($index["month"] !== $previousmonth) {

instead of

if ($index["month"] != $previousmonth) {

, because

0 == ''

, but

0 !== ''

. Because,

$index['month'] != ''

is

false

, your if statement fails.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.