Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic jpgraph from mysql database 1

Status
Not open for further replies.

scriggs

IS-IT--Management
Jun 1, 2004
286
GB
I am relatively knew to php and am trying to use it to analyse data from a db and create dynamic graphs.

As an example, I have a simple table

ID Area Search
1 North East Hair
2 North East Music
3 North West Food
4 North West Music

I have been playing with jpgraph and can create a graph which is driven from a query, something like: "SELECT Area, COUNT(*) as CountUp FROM php.`data` group by Area"

What I want is to drive this PHP function from an HTML form, so that the user can change selecy AREA, SEARCH, etc and the SQL is updated and refreshses the graph.

I'm a bit stuck on where to start with this?


My code at the moment is:
Code:
<?php
   include ("jpgraph.php");
   include ("jpgraph_pie.php");


      $host = 'localhost';
      $user = 'root';
      $bdd = 'PHP';
      $password  = '*****';

      mysql_connect($host, $user,$password) or die("error connecting to server");
      mysql_select_db($bdd) or die("error connecting to database");

   $SQL = "SELECT Area, COUNT(*) as CountUp FROM php.`data` group by Area";
   $RESULT = mysql_query($SQL);
   if ($myrow=mysql_fetch_array($RESULT)) {
      do {
         $data[] = $myrow["CountUp"];  //It would not create the graphs without using '[]'
         $data_names[] = $myrow["SHORT_DESC"].$myrow["Area"];
      }while ($myrow=mysql_fetch_array($RESULT));
   }

// Create the Pie Graph.
   $graph = new PieGraph(520,400,$filename,60);
   $graph->SetShadow();

// Set A title for the plot
//   $graph->title->Set($PIE_TITLE);
   $graph->title->Set ("Area Split");
   $graph->title->SetFont(FF_VERDANA,FS_BOLD);

// Create
   $p1 = new PiePlot($data);
   $p1->SetCenter(0.35,0.5);
   $p1->SetLegends($data_names);

   $p1->SetTheme("earth");

   $graph->Add($p1);
   $graph->Stroke();
?>
 
it's a question of capturing the incoming choices in the $_POST superglobal (or $_GET - depending on your preferences) and the constructing your sql based on that value.

sorry if that's either too basic or too vague - if you provide a specific question i can provide some example code for you to build on.
 
Hey jpadie

Thanks for the quick reply.

I want to have a form with drop down selection lists, something like below, that allows the user to select the criteria for SQL. Preferably this will submit to itself so that the user can keep changing the criteria and refresh the graph output.

What I am really struggling is how to pass this to my next page....

phpform.php
Code:
<form name="form" method="post" action="?????.php">     
<h4>Select criteria</h4>        
<p>Area<br>          
<select name="select">
            <option value="North East" selected="true">North East</option>
            <option value="North West">North West</option>
            <option value="South East">South East</option>
            <option value="South West">South West</option>
          </select>
        
</p>

<p> 
          
<input type="submit" name="Submit" value="Submit">
          <input type="reset" name="Reset" value="Reset">
        
</p>
</form>

I want the selection to be used in generating the graphs. I have this working by hard coding into the <img> tag, something like this I have got for that at the moment.

mysql.htm - hard coded the VAR=SearchTerm, need this fed from form above....
Code:
<img src="mysqld.php?VAR=SearchTerm">

mysqld.php - does the graphing, happy with this :)
Code:
<?php
   include ("jpgraph.php");
   include ("jpgraph_pie.php");

   $host = 'localhost';
   $user = 'root';
   $bdd = 'PHP';
   $password  = '*****';

   mysql_connect($host, $user,$password) or die("Error connecting to server");
   mysql_select_db($bdd) or die("Error connecting to database");

   $SQLVAR = $_REQUEST['VAR'];
   $SQL = "SELECT $SQLVAR, COUNT(*) as CountUp FROM php.`data` group by $SQLVAR";
   $RESULT = mysql_query($SQL);
   $PIE_TITLE = $SQLVAR . ' Chart';
   if ($myrow=mysql_fetch_array($RESULT)) {
      do {
         $data[] = $myrow["CountUp"];  //It would not create the graphs without using '[]'
         $data_names[] = $myrow["SHORT_DESC"].$myrow[$SQLVAR];
      }while ($myrow=mysql_fetch_array($RESULT));
   }

// Create the Pie Graph.
   $graph = new PieGraph(520,400,$filename,60);
   $graph->SetShadow();

// Set A title for the plot
   $graph->title->Set($PIE_TITLE);
   $graph->title->SetFont(FF_VERDANA,FS_BOLD);

// Create
   $p1 = new PiePlot($data);
   $p1->SetCenter(0.35,0.5);
   $p1->SetLegends($data_names);

   $p1->SetTheme("earth");

   $graph->Add($p1);
   $graph->Stroke();
?>

Thanks....
 
aha - easier than i thought it would be!

change the name of the select element to "VAR" and point the form action at the mysqld.php script.

that's all, I think!
 
Thanks again.

Tried your suggestion, but i get an error (Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\Inetpub\ on line 22
). I think it misses a step. The form needs to create the second html page that calls an image statement which in turn calls mysqld.php:

Code:
<img src="mysqld.php?VAR=SearchTerm">

I would also like the form page to submit to itself is possible so that the drop down list is always available so that you can resubmit and rerun the graph.

I'm still trying me end, but don't know enough to get it going...
 
no. the error you are getting is a mysql error. something in your query is wrong.

no problem with form submitting to itself. just put the following code at the start of the page. I understand what you are trying to achieve now.

Code:
<?
if (isset($_POST['submit'])):
	$criteria = $_POST['criteria'];
else:
	$criteria = "North East";
endif;
?>
<div>
<img src="mysqld.php?VAR=<?=$criteria?>">
</div>
<form name="form" method="post" action="<?=$_SERVER['PHP_SELF']?>">     
<h4>Select criteria</h4>        
<p>Area<br>          
<select name="select">
	<option value="North East" <? $criteria==="North East" ? echo "selected=\"selected\"":echo ""?>>North East</option>
	<option value="North West" <? $criteria==="North West" ? echo "selected=\"selected\"":echo ""?>>North West</option>
	<option value="South East" <? $criteria==="South East" ? echo "selected=\"selected\"":echo ""?>>South East</option>
	<option value="South West" <? $criteria==="South West" ? echo "selected=\"selected\"":echo ""?>>South West</option>
</select>
<br/> 
          
<input type="submit" name="submit" value="Submit">
          <input type="reset" name="Reset" value="Reset">
        
</form>
 
Only just picked up your reply, it was late last night. I have tried the code you suggested, but it doesn't seem to work for me. I get the error:

'Parse error: parse error, unexpected T_ECHO in c:\Inetpub\ on line 15'

Line 15 is:
Code:
    <option value="North East" <? $criteria==="North East" ? echo "selected=\"selected\"":echo ""?>>North East</option>

I don't really know what this line does, I don't think criteria, etc should really be in it? The criteria should just be in line 9 for the call to the graph I think?
 
my fault. i typed these out too quickly. $criteria should be in these option declarations. replace the select block with the following. my apologies:

Code:
<select name="select">
    <option value="North East" <? if ($criteria==="North East") echo "selected=\"selected\""; else  echo "" ;?>>North East</option>
    <option value="North West" <? if ($criteria==="North West") echo "selected=\"selected\""; else echo "" ;?>>North West</option>
    <option value="South East" <? if ($criteria==="South East") echo "selected=\"selected\""; else echo "" ;?>>South East</option>
    <option value="South West" <? if ($criteria==="South West")  echo "selected=\"selected\""; else echo "" ;?>>South West</option>
</select>
 
No need to apologies, I think we are getting there.

The page now runs without an error, but does not quite work. The criteria does not seem to be working. Whatever I select from the option box, after I press submit it reverts back to 'North East', not what I have selected.

Plus the image is not working, when the page is loaded, the line calling the image shows:

Code:
<img src="mysqld.php?VAR=">

So the criteria is not working through the page.

Any ideas?
 
can you post the code you are actually using? quite often the smallest changes (i.e. case) make the difference !
 
Appreciate that, I just copied your code to the clipboard, pasted into notepad and saved as mysqlform.php. Copy code is below:

Code:
<?
if (isset($_POST['submit'])):
    $criteria = $_POST['criteria'];
else:
    $criteria = "North East";
endif;
?>
<div>
<img src="mysqld.php?VAR=<?=$criteria?>">
</div>
<form name="form" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<h4>Select criteria</h4>
<p>Area<br>
<select name="select">
    <option value="North East" <? if ($criteria==="North East") echo "selected=\"selected\""; else  echo "" ;?>>North East</option>
    <option value="North West" <? if ($criteria==="North West") echo "selected=\"selected\""; else echo "" ;?>>North West</option>
    <option value="South East" <? if ($criteria==="South East") echo "selected=\"selected\""; else echo "" ;?>>South East</option>
    <option value="South West" <? if ($criteria==="South West")  echo "selected=\"selected\""; else echo "" ;?>>South West</option>
</select>
<br/>

<input type="submit" name="submit" value="Submit">
          <input type="reset" name="Reset" value="Reset">

</form>
 
Think I solved it myself - the select was called 'select', not 'criteria'. Thanks for your help, have a star and I will keep on trying. Might have some more questions later....
 
i'm being really dumb. so sorry again. trying to do more than i can justify!

use this code instead. i had misnamed the select element and also the $criteria needs to be urlencoded to passthrough the img tag.
Code:
<?
if (isset($_POST['submit'])):
    $criteria = $_POST['criteria'];
else:
    $criteria = "North East";
endif;
?>
<div>
<img src="mysqld.php?VAR=<?=urlencode($criteria)?>">
</div>
<form name="form" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<h4>Select criteria</h4>
<p>Area<br>
<select name="criteria">
    <option value="North East" <? if ($criteria==="North East") echo "selected=\"selected\""; else  echo "" ;?>>North East</option>
    <option value="North West" <? if ($criteria==="North West") echo "selected=\"selected\""; else echo "" ;?>>North West</option>
    <option value="South East" <? if ($criteria==="South East") echo "selected=\"selected\""; else echo "" ;?>>South East</option>
    <option value="South West" <? if ($criteria==="South West")  echo "selected=\"selected\""; else echo "" ;?>>South West</option>
</select>
<br/>

<input type="submit" name="submit" value="Submit">
          <input type="reset" name="Reset" value="Reset">

</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top