Stuck on this bit of PHP

Soldato
Joined
14 Apr 2003
Posts
4,950
Location
Deepest Yorkshire
So I'm making a script that reads test results from a database and displays the results, then uses PostGraph ( http://www.qualityunit.com/postgraph/ ) to show the results as a graph, however when I call the function I get the following error:

Code:
Warning: Cannot modify header information - headers already sent by (output started at 
/Applications/xampp/xamppfiles/htdocs/calproject/header.php:8) in 
/Applications/xampp/xamppfiles/htdocs/calproject/postgraph/postgraph.class.php
 on line 291
‰PNG  ��� 
IHDR��&��J���Û>‰É���PLTEÿÿÿHkHkçç窪ª������ÿÿÿá˼��"IDATxœí[’›8@™ýO&+H²‚W±~X«Øÿb 0 {±1„„ÎéNu7ÁF>¾º€VU��”…iÛöì2¤†iì7ÌÁ‰¤wbëiqÓãò‰i¬—ŠÄÒã*ÎCFK%z‚ N$ÎùdAœw��������2¦m*svÃ<¾š»<Œ!PO©[Þï%åÄ6Û·ñÔ®i")'öóÙN™«’´“¾îÄîóªo ;9iž×=e'çÌó²G 
çØ$æ¾¥å¤mSèMÊÉNpâÀ‰'’щÑ?§<šïåÿ±ÛÎvÒ6êòE;iõ³Ûq‚dzîèk´ídeéÓ¢¬P¸òÉN$_C6!ÇNàDòõ¬7Ôò‰$Ïsq}뮞œÌ¯ÙL?ý-Y'Ý-ä ÙçýN5ϱm{M¢Oã$ä 
ÙÉÉ,Ÿ¸UšŠw⋇º±ðS'!Ç›ON–ù$í8éîœTË»âÔ„e}Ž$N×l¶0ésqXN»Žõ¦´¥çÄ4±ïk/KâäæIJЉwÍÇÉ}~ÙµÕ‰¿íO€¢—O–ÓAÓt¢.Ÿtûrl\'yôeÄt²¼/ÆÉ)9Ö#A'Ù´QÇuB>Ñï‹qò‚NÔ—…œxàD‚I¦NLÈÏ"ËÔI;^Õâä&fßV´S)ý;aú¼²qÒ¯0\_F6Ncp2 £Úç•“Å„°}^Ù81ñÚ³q²BáN¢µ=âD’‰“¨}£8‘dâ$j_F6NÈ'œ‹%ÏëØx}£ù8!Ç>yÖU 
NpÒCÝ‘c%œ‹%Ó ·fb¶N¨; œwD”M99±ËÂ¥®Ùp"œõ9»8‘\ÉÉAU' Wrr8‘àD‚ 
N$8‘\ÂÉoeÇo€ 
N$8‘àD‚Éš“Su$'ÍqqboWëÙ=k®NƱm8q:櫨dëÄõÒçu¿NœÖ¿ãV|¼DœØçeWdÂÉëä u'¯ñ'‘œà'œHp"Á‰$°*´8Á‰N$8‘àD‚ NúÛßn¶(Núf’ùp!œôÃìæÃTq2ÄȬM 
'ÖÆ²ñRNöµ³u×Îxº“ÅÂÑßsrT{ìÙNê=DÏU9ÛÉ®A³«ýÅÇôyîdÏxó«ÇÉŽ0¹º“]szöçX•ïÒúÙNv±ÿ\ÜÝ–i}si³u2ð.Nòtò¦>}Óɇ]ãi8©:y<÷‡]ãi8 'ݽ¾ÏÓ:N$‰9QwÄÉš“úÍD‰NÞœêäí}CæNfWZ[Ô‹›î½/5a'÷éMÇIéN´q"wÜïd™c¿Uˆ4ìȱK¾Uˆ4ÌØáäK{îŸJ!¾±£Û&|·£äN6;y±þN±NšõöØR¼j·/Û‰ëôžæ—²í·Ýöß‘;þÒv<ü(êŽMoâDù?m[õïÑ;Æ9Šº£¶*‰ʘc£"'ã¹8J!2qòâyJw��p ÚâÚrWFÛQÙf´åµoÞQ/Žvð¦’K—ý9_¡–V{*ÚJiúƒôRm=жcÛ¯íîGêkõuï^¢wcL4êÛ¯=V{B*¨ªe]½t²'úÛôŠ*¥Õ6êkÊiïªúúµÇª¿éäÓº³5$VJ+7m®xÛ¢f³“ÃDõíÙïè½=Ç>Š(v}¼Yò¶YY!�à,óæð—ºøv9LNìoð"Jtbšþ?.ÜW3^ñØåêíwëbE½º*£“Ç/Ö‰»5kû¿çu§U/Æ®Êpåh—BbQiF'E]`¶CT˜ÑÉ Å“‚êŽýá×¶wÒWw¦k˜¡ ¹-éóì|#�����ÀÇü|Ü£ÈiW"Ã����IEND®B`‚

this obviously means that headers are being sent twice, then the rest of the crap is the actual image.

if I use the external example file it outputs the image perfect, however I have no way to send the array of data to it.

If I remove the header line from the PHP it just outputs the image but the browser wont display it as such, is there a way to do this?

This is the printImage function which isn't working properly.

Code:
  /**
    * draw graph to the output
    *
    * @param null
    * @returns nothing
    */
    function printImage()
    {
        header("Content-type: image/jpeg");
        ImagePNG($this->img);
    }

This is what is in my code at the moment, after everything else
Code:
include('postgraph/postgraph.class.php'); 

$data = array(1 => 0, 1.2, 2.5, 4.8, 16, 20, 22, 17, 7, 2, 1, 0);

$graph = new PostGraph(550,330);

$graph->setGraphTitles('My Graph', 'x axis title', 'y axis title');

$graph->setYNumberFormat('integer');

$graph->setYTicks(10);

$graph->setData($data);

$graph->setXTextOrientation('horizontal');
    
$graph->drawImage();

$graph->printImage();

Any ideas?

Hamish
 
are you trying to view the image directly in your browser or is it embedded in a page using <img> tags? what's in header.php? :)
 
I'm trying to embed it in a HTML page, it works fine on its own.

However I don't know the code to embed anything but an external file in <img> tags.

header.php just contains page headers - stylesheets, doctypes, HTML head and stuff like that.
 
you just embed the path to php script as if it were an image (well actually that's all it is). easy. :)

Code:
<img src="/path/to/graph.php" alt="graph" />
 
Last edited:
marc2003 said:
you just embed the path to php script as if it were an image (well actually that's all it is). easy. :)

Code:
<img src="/path/to/graph.php" alt="graph" />


do you mean like this? :

Code:
echo('<br/><img class="centre" src="'.$graph->printImage().'" />');

cos it still brings up the same error
 
Still not sure what you mean, the file is a class so including it will just include the class, you need to call it's methods in order to manipulate it don't you?
 
Hamish said:
Still not sure what you mean, the file is a class so including it will just include the class, you need to call it's methods in order to manipulate it don't you?

i've donwloaded it myself to have a look and you are calling it's methods using image.php? that's the file we treat as an image. now you embed image.php in another html page...

Code:
<img src="image.php" />

there really is nothing to it. :D
 
Yeah, the Image.php class is an example, ideally I would call the methods from the current page as it allows me to input my data easier, unless there is a way of sending an array through "GET"
 
so let's say your current page is index.php and this contains all your data code. why not put that in another external file called data.php? then you can include that in both index.php and image.php :)

as an example, my sig is 2 scripts pic.php and text.php - they both include another file called foo.php which gets the info to be displayed. :)
 
OK, I just copied the SQL into the image file, however the SQL is all executed twice which isn't ideal. Anyway at least it works

Thing is i'm not used to PHP, more of a JAVA boy.

The graph needs information in the form:

$data = array(1 => 0, 1.2, 2.5, 4.8, 16, 20, 22, 17, 7, 2, 1, 0);


I'm trying to insert data into it as I go along starting with:

$data = array(1 => 0);

then incrementing each position as I go along:

$data[$correct] += 1;

which kind of works. except the graph only shows values up to the maximum value given rather than the maximum value possible. Is it possible to force an array of a given size?
 
marc2003 said:
well i'm bit of a php noob myself but i think you should take a look at sessions so you only have to execute your sql once? :)

http://uk3.php.net/manual/en/function.session-start.php



that would require tinkering with the class you downloaded? i think. :p



eh? :confused:

Hi, Sessions are for storing session data, not passing data between scripts, I already use them to log users in.

Also the array problem is not with the program, it just read to the end of the array, if I can make the array longer but contain nothing then it will look right.

e.g. at the moment I would have to create an array like:

$data = array(1 => 0,0,0,0,2,0,0,0);

manually if I wanted the graph to carry on after the value as by making it automatically it would make something like:

$data = array(1 => 0,0,0,0,2);

I know how many positions I want in the array, is there any way to tell PHP I want the array to be that long, or can I just loop through the array and set everything?
 
Hamish said:
Hi, Sessions are for storing session data, not passing data between scripts, I already use them to log users in.

well reading that page i linked certainly looks like you can use it to pass data. well it does to me anyway... :p

as for the array, i guess you could make one like this...

Code:
for ($x = 1; $x <= 10; $x++) {
	$data[$x] = 0;
}

that gives you an array starting at 1 like that class needs with 10 values all set to 0. i guess you could incorporate your method for adding your own values within the loop? :)
 
marc2003 said:
well i'm bit of a php noob myself but i think you should take a look at sessions so you only have to execute your sql once? :)

http://uk3.php.net/manual/en/function.session-start.php



that would require tinkering with the class you downloaded? i think. :p



eh? :confused:

Hi, Sessions are for storing session data, not passing data between scripts, I already use them to log users in.

Also the array problem is not with the program, it just read to the end of the array, if I can make the array longer but contain nothing then it will look right.

e.g. at the moment I would have to create an array like:

$data = array(1 => 0,0,0,0,2,0,0,0);

manually if I wanted the graph to carry on after the value as by making it automatically it would make something like:

$data = array(1 => 0,0,0,0,2);

I know how many positions I want in the array, is there any way to tell PHP I want the array to be that long, or can I just loop through the array and set everything?
 
You can pass arrays in the query string.

foo.php?foo[]=one&foo[]=two&foo[]=three

Code:
echo $_GET['foo'][2]; // prints "two"

Hamish said:
Hi, Sessions are for storing session data, not passing data between scripts, I already use them to log users in.

Sessions are for passing data between scripts, actually. That's a perfectly valid use of them, anyway.
 
Back
Top Bottom