0

I want to use the pChart Library (https://github.com/bozhinov/pChart2.0-for-PHP7) to draw some graphs based on a MySQL Dataset. Therefore I'm including a PHP file which contains the MySQL Query and the pChart code for drawing the graphs in another page. This is the page structure:

main page ski.php
|- msqry.php: MySQL Query and pChart code

Currently I'm struggling with including the necessary pChart class files. On top of msqry.php I'm including the classes:

$filenameclass1 = $_SERVER["DOCUMENT_ROOT"] . "/pchart/pChart/pColor.php";
if(file_exists($filenameclass1)) {
    include($_SERVER["DOCUMENT_ROOT"] . "/pchart/pChart/pColor.php"); 
} else {
    echo "Class pColor not found";
}
 
$filenameclass2 = $_SERVER["DOCUMENT_ROOT"] . "/pchart/pChart/pDraw.php";
if(file_exists($filenameclass2)) {
    include($_SERVER["DOCUMENT_ROOT"] . "/pchart/pChart/pDraw.php"); 
} else {
    echo "Class pDraw not found";
}

$filenameclass3 = $_SERVER["DOCUMENT_ROOT"] . "/pchart/pChart/pCharts.php";
if(file_exists($filenameclass3)) {
    include($_SERVER["DOCUMENT_ROOT"] . "/pchart/pChart/pCharts.php"); 
} else {
    echo "Class pCharts not found";
}

All three files exist and are included successfully (since I'm not getting any echos saying 'Class xxx not found'). When creating a new pDraw object:

$anab_dgr = new pDraw(700, 230);

I'm getting this error: "Fatal error: Uncaught Error: Class 'pDraw' not found in msqry.php ..."

So obviously the class cannot be found by PHP although the file exists and is included within the current file. What am I missing here?

Any help is very much appreciated.

Edit I forgot to mention that the examples provided in the above github repository are working as expected.

bear87
  • 83
  • 1
  • 11
  • "_What am I missing here?_" A namespace maybe? `$anab_dgr = new pChart\pDraw(700, 230);` Or, use `use pChart\pDraw;` at the top of your file, like in the examples – brombeer Jul 18 '20 at 07:18
  • When adding a namespace or using use I'm getting the same error: "Fatal error: Uncaught Error: Class 'pChart\pDraw' not found ..." – bear87 Jul 18 '20 at 07:32
  • I have to correct my first comment. After adding "use pChart\pDraw", pDraw is now found. But instead of the first error I'm now getting "Fatal error: Uncaught Error: Class 'pChart\pData' not found in /pchart/pChart/pDraw.php on line 158". I didn't change a thing in pDraw.php compared to the original file provided on github. – bear87 Jul 18 '20 at 08:08
  • Because you don't load/include `pChart\pData` anywhere. Take a look at the examples, they use `require_once("bootstrap.php");` to "autoload" all pChart related classes – brombeer Jul 18 '20 at 08:13

0 Answers0