Aero77 Posted May 16, 2014 Share Posted May 16, 2014 Newbie here, would love some help to find out what I'm doing wrong on this code. I'm using phpChart to make a graph. This is my code, but the graph does not appear. <?php require_once("phpChart_Lite/conf.php"); include 'connection.php'; ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>phpChart - Basic Chart</title> </head> <body> <?php $query = mysqli_query($con, "SELECT krl FROM diesel WHERE sted = 'Borgeskogen' ORDER BY dato ASC"); // set array $price = array(); // look through query while($row = mysqli_fetch_assoc($query)){ // add each row returned into an array $price[] = $row['krl']; } //$pc = new C_PhpChartX(array(array(11, 9, 5, 12, 14)),'basic_chart'); $pc = new C_PhpChartX(array($price),'basic_chart'); $pc->set_animate(true); $pc->draw(); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/ Share on other sites More sharing options...
bsmither Posted May 16, 2014 Share Posted May 16, 2014 I don't know PHPChart, but... Maybe because $price is already an array? You are sending an array, of which the only element is the array $price, into the class. Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479757 Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 On line #27 I commented out the working part which came with the example. I'm trying to make my own array from the values in the database. Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479758 Share on other sites More sharing options...
Barand Posted May 16, 2014 Share Posted May 16, 2014 Put error_reporting(-1); at top of the file. As it creates a chart I suspect it may create an image file, so you may need to strip out all the HTML code so it is pure php only Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479762 Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 its not an image file. look at this example http://phpchart.org/phpChart/examples/basic_chart.php?iframe=true&width=850&height=500 its just the same as mine Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479763 Share on other sites More sharing options...
Barand Posted May 16, 2014 Share Posted May 16, 2014 (edited) You are passing an array and a string. You should pass an array containing an array, and a string $pc = new C_PhpChartX(array(array($price)),'basic_chart'); edit : removed final ) Edited May 16, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479765 Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 I thought the $price became an array on line #24 Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479766 Share on other sites More sharing options...
Barand Posted May 16, 2014 Share Posted May 16, 2014 You're quite right. I'll shut up :-) Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479768 Share on other sites More sharing options...
Aero77 Posted May 16, 2014 Author Share Posted May 16, 2014 When I add the $price array in there, this line in the "debugger" looks like this ___chart1= $.jqplot("__chart1", [["13.48","13.74","13.87","13.87","13.87","13.87","13.87","13.87","13.75","13.65","13.60","13.60"]], ___chart1_plot_properties); and with the original array from the example it looks like this ___chart1= $.jqplot("__chart1", [[11,9,5,12,14]], ___chart1_plot_properties); It looks like my array is getting some of these tags " added, can that be the problem ? Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479769 Share on other sites More sharing options...
Barand Posted May 16, 2014 Share Posted May 16, 2014 Is phpChart putting them in or you? echo '<pre>', print_r($price, 1), '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479771 Share on other sites More sharing options...
Solution bsmither Posted May 16, 2014 Solution Share Posted May 16, 2014 Ah! I think the database is returning strings instead of floats. So, the line might work better as: $price[] = (float)$row['krl']; Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479773 Share on other sites More sharing options...
Aero77 Posted May 17, 2014 Author Share Posted May 17, 2014 Ah! I think the database is returning strings instead of floats. So, the line might work better as: $price[] = (float)$row['krl']; Thanks. That solved it! Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479817 Share on other sites More sharing options...
Barand Posted May 17, 2014 Share Posted May 17, 2014 Are you storing your prices as varchar in your database? Should be something like DECIMAL(10,2) Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479818 Share on other sites More sharing options...
Aero77 Posted May 17, 2014 Author Share Posted May 17, 2014 Yes they are stored as varchar. Is it possible to change that without loosing data ? Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479819 Share on other sites More sharing options...
Barand Posted May 17, 2014 Share Posted May 17, 2014 Yes EG CREATE TABLE IF NOT EXISTS testprice ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, price VARCHAR(10) ); INSERT INTO testprice (price) VALUES ('125.99'), ('15.99'), ('12.49'), ('1.00'), ('0.50'); SELECT * FROM testprice; +----+--------+ | id | price | +----+--------+ | 1 | 125.99 | | 2 | 15.99 | | 3 | 12.49 | | 4 | 1.00 | | 5 | 0.50 | +----+--------+ Now alter the column type ALTER TABLE `test`.`testprice` CHANGE COLUMN `price` `price` DECIMAL(10,2) NULL DEFAULT NULL; SELECT * FROM testprice; +----+--------+ | id | price | +----+--------+ | 1 | 125.99 | | 2 | 15.99 | | 3 | 12.49 | | 4 | 1.00 | | 5 | 0.50 | +----+--------+ Quote Link to comment https://forums.phpfreaks.com/topic/288548-phpchart-and-mysql-goes-wrong/#findComment-1479824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.