require_once function

288 Views Asked by At

I have this set of codes from JpGraph to help me with creating a bar chart.

<?php
require_once ('src/jpgraph.php');
require_once ('src/jpgraph_bar.php');
$datay=array(1992,1993,1995,1996,1997,1998,2001);

// Size of graph
$width=400;
$height=500;

// Set the basic parameters of the graph
$graph = new Graph($width,$height);
$graph->SetScale('textlin');

$top = 60;
$bottom = 30;
$left = 80;
$right = 30;
$graph->Set90AndMargin($left,$right,$top,$bottom);

// Nice shadow
$graph->SetShadow();

// Setup labels
$lbl = array("Andrew\nTait","Thomas\nAnderssen","Kevin\nSpacey","Nick\nDavidsson",
"David\nLindquist","Jason\nTait","Lorin\nPersson");
$graph->xaxis->SetTickLabels($lbl);

// Label align for X-axis
$graph->xaxis->SetLabelAlign('right','center','right');

// Label align for Y-axis
$graph->yaxis->SetLabelAlign('center','bottom');

// Titles
$graph->title->Set('Number of incidents');

// Create a bar pot
$bplot = new BarPlot($datay);
$bplot->SetFillColor('orange');
$bplot->SetWidth(0.5);
$bplot->SetYMin(1990);

?>

However, this will only work if I put it right at the top of my code. If I put it anywhere else, it will fail to display. Is there any way to overcome this so that if I put the code specifically at one place for example under it will appear there? Also, I'll be using some data from my own database as values for this graph.

Thank you.

4

There are 4 best solutions below

0
On

Well, you can't escape the fact that when you call new Graph(), the class should already exist, so if you include the files after that line it'll never work.

0
On

Requires/includes are resolved at runtime, not during parse. This allows them to be invoked dynamically. Therefore, they must be completed before any of their code can be referenced.

Alternative methods to look at are autoload, spl_autoload. This allows class files to be loaded on first reference.

However, from your comments, it appears that the issue is the usage of JpGraph requiring the sending of headers. You'll need to check studentcourse.php to see if any output is generated (including inadvertent whitespace).

0
On

JpGraph creates an image, which is displayed. You can't output text and images at the same time.

1
On

This generates an image, right?

I would put this in a file by itself, and then on the page where you want the graph to show up, just do:

<img src="myGraph.php">