phparrayspchart

Why can't I assign any variable to a function return value in pChart?


I'm trying to assign a return value from a method to an array and in a second step to place this array into the addPoints() method of pChart.

Right now I'm using the following code:

$dataUsage = array();
for ($i = 1; $i < 31; $i++) {
        $dataUsage[$i - 1] = getDailyUsage($i);
}
$dataUsage[30] = getDailyUsage(31);

The getDailyUsage method looks like this:

function getPeriod() {
    return date("Y-m");
}

function getDailyUsage($day) {
        $var = getPeriod() . "-" . $day;
        global $pdo;
        $sql = "SELECT COUNT(  `id` ) FROM  `logs` WHERE timestamp = '$var'";
        $q = $pdo->prepare($sql);
        $q->execute();
        $q->setFetchMode(PDO::FETCH_ASSOC);
        $number = $q->fetchColumn(0);
        return $number;
}

To display the data into the pChart I'm running this:

$myData->addPoints($dataUsage, "Serie2");

As output I'm getting the default html icon as if I'm not finding the picture I'm looking for.

What really makes me curious is that I don't get any errors if I run this code:

$array = array();
for ($i = 1; $i < 31; $i++) {
    $array[$i - 1] = $i;
}
$array[30] = 31;

$myData->addPoints($array, "Absissa");

Since this is my first post I'm looking for tips to improve my questions, so feel free to leave a comment.

Edit: If I use:

$dataUsage = array();
for ($i = 1; $i < 31; $i++) {
        $dataUsage[$i - 1] = $i;
}
$dataUsage[30] = $i;

I can draw the image. But I couldn't even run it when I tried:

$myData->addPoints(array(getDailyUsage(1), getDailyUsage(2)), "Absissa")

Solution

  • I got help for my problem and want to share the solution: I had to check for the html source in the browser. The .php file which displays the picture showed the error. Problem was that the file, which contains the connection information wasn't included.

    I recommend you to look at the picture / file if you're working with pChart to avoid those errors.