Jump to content

This code works on its own, but not when used in another page


Moron

Recommended Posts

<?php

require_once('class_PieChart.php'); 

$pie = &new PieChart(); 

// setup our data; value corresponds to the percentage 

$piedata = array( array('value'=>10,'title'=>'Fruit'), array('value'=>25,'title'=>'Vegetables'), array('value'=>40,'title'=>'Meat'), array('value'=>10,'title'=>'Dairy'), array('value'=>15,'title'=>'Pepsi'), );

// pass the data to the pie chart class 

$pie->data($piedata); 

// create a 170x110px image with a 150x150px pie chart, with a 

// drop shadow under the pie chart, antialiasing enabled, and legend disabled 

$pie->create_image(170,110,150,150,true,true,false); 

// display the image (outputs appropriate headers to the browser for a PNG 

// image, then displays the PNG) 

$pie->display(); 

// clean up 

$pie->destroy_image();

?>

 

When I run it on its own, it displays a pie chart (using hardcoded variables). When I embed this exact same code in one of my pages, I get the ASCII representation of the pie chart, not the actual chart image.

 

Ideas?

 

Link to comment
Share on other sites

Shot in the dark ...

 

Are the script that causes the problem and the script that works in the same folder? If not, try putting the script that doesn't work in the same folder as the script that works. Test.

 

If this solves your problem, then probably class_PieChart.php tries to include the script that does the rendering but can't find it. Since it can't find it, it does some default rendering.

 

This is just a guess.

Link to comment
Share on other sites

I think you need to be saving that in its own file and referencing it in an image tag...

<img src='pie.php' alt='pie chart' title='pie chart' border='0px' />

 

When I do that, I get nothing but a red 'x.' Doing it this way, I at least get the pie chart image.

Link to comment
Share on other sites

Are the script that causes the problem and the script that works in the same folder? If not, try putting the script that doesn't work in the same folder as the script that works. Test.

 

No, they are both the same script. I take the code I posted above and put it at the beginning of my PHP file and it works. That is, it shows the pie chart, but it shows nothing else on the page, just the pie chart.

 

Conversely, if I put the code elsewhere on the page, the rest of the page works but the code above shows an ASCII representation of the JPG file, i.e. garbage.

 

Link to comment
Share on other sites

SemiApocalyptic is correct. The ONLY way to put an image (a static one or one dynamically produced by php) on a web page (without browsing directly to the url of the image) is using a proper HTML <img scr="..." tag.

 

If you got a red-x, you will need to troubleshoot why.

 

Was the URL in the src="..." parameter the php file that you can browse to and get the chart to display?

 

The garbage you are getting means that a proper mime type header for the image is not being output. The class function you are using should have a way of outputting the header.

 

Edit: Actually, I found the script you are using and it does output a PNG header. That would mean the grabage output is due to you outputting the image directly in the HTML on the page instead of using an <img src="..." tag.

Link to comment
Share on other sites

I played with that script and it contains a bug that triggers a php error when it references an array element that does not exist without first testing if it exists. The php error output "breaks" the image header, in the same way that outputting content to a browser breaks things like cookie/session/redirect headers.

 

It may very well be that if you call the script and provide values instead of using defaults, that it works without error. The offending line (there could be more) is line 98 in the class_PieChart.php file -

 

Change this -

 

if ($item["color"]) {

 

to this -

 

if (isset($item["color"])) {

 

Note: If you have previously browsed to a url where the image appears as a broken image (red-x), and you correct the problem so that the image actually works, you will need to hit your browsers refresh button to get the actual image to be fetched (or clear you browser's cache.)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.