Link to home
Start Free TrialLog in
Avatar of deibel
deibelFlag for Germany

asked on

Draw chart with timestap from array

Hello Experts,

i want to make a chart with chart.js in php. the values are stored in sqlite db file.
i dont know how to get this work. i think the problem is in timestamp value.
can someone help me fix this?

regards


ps: you need to rename the file p1_db to p1.db from the zip file i uploaded. db files are not alowed to be uploaded.
chart.zip
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

I think you'll get more help here if you post a specific question about a specific problem. Telling us 'it doesn't work' and then posting up a zip of your app isn't much to go on.

If I were you, I'd break this down into the smaller moving parts of your application and then address each issue one at a time. For example, start by creating your chart with some static, known-good data. This would allow you to test your chart code out without having to worry about the database. If you have problems with that part, then ask a question about that part only.

Once you've got that working, write the code that accesses your Database and retrieves your records. Test that out without worrying about your chart - i.e. run your script and make sure it returns the data.

Once you've got these parts working on their own, then you can start to tie them together.

Tackling this as one big problem will only lead to frustrations.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deibel

ASKER

ok
basicly i got it runing  without arrays and without the values from the database.

i think the problem is the $labels[] array and the join of the array values at $labels = join(',', $labels);  


<?php
$dbh = new PDO("sqlite:p1.db");
$result = $dbh->query("SELECT * FROM tomaten");


$labels = array();
$data = array();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
   
    $labels[] = $row['zeit'];
    $data[]   = $row['value'];
     
 }
 
$labels = join(',', $labels);
$data   = join(',', $data );
?>

var data = {
            labels: [ <?php echo $labels; ?> ],
                datasets: [
                    {
                        label: "Frequenz",
                        data: [ <?php echo $data; ?> ],
                        backgroundColor: "blue",
                        fill: false
                    } 
                    ]
        };

Open in new window

Avatar of deibel

ASKER

array_walk($labels, function(&$x) {$x = "'$x'";});
Fatal error:  Uncaught Error: Call to a member function fetch() on boolean in C:\xampp\htdocs\chart\index.php:20
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deibel

ASKER

yes, i put it exactly as you said - after the while() loop
OK. Well in that case you have a problem with your database. This error:

Call to a member function fetch() on boolean

Basically means that this line:

$result = $dbh->query("SELECT * FROM tomaten")

returned false, instead of a PDOStatement. It's likely being caused by a failure to connect to your DB. Double check the path to your db file. With SQLite, when you try and connect to a non-existant DB, instead of throwing an error like normal, it will actually create a new file and connect to that. Obviously the new file won't contain your data, so any attempt to query it will throw an error.

Do a file_exists check before using your DB:

$dbFile = "./p1.db";
if (!file_exists($dbFile)) die("Couldn't find the DB: $dbFile");
$dbh = new PDO("sqlite:$dbFile");

Open in new window

Avatar of deibel

ASKER

ohh sorry..what a s***t
my fault
i accidentaly renamed the db file.
now it works
thanks you
your are the best :)
No worries. Glad I could help.