Jump to content

PHP Graph


karenn1

Recommended Posts

Hey everyone,

 

I have a SQL query that pulls data from my database:

 

$sql = "SELECT Log_Date, Log_Reading, Channel, Device_Code, Zone_Code FROM datalog
		WHERE Zone_Code = '".$_REQUEST['zone']."'
		AND Log_Date BETWEEN '".$_REQUEST['day_from']."' AND '".$_REQUEST['day_to']."'
		AND (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan1_Reference='".$_REQUEST['reference']."') AND Channel=1)   
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan2_Reference='".$_REQUEST['reference']."') AND Channel=2)  
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan3_Reference='".$_REQUEST['reference']."') AND Channel=3)  
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan4_Reference='".$_REQUEST['reference']."') AND Channel=4) 
		ORDER BY Channel, Log_Date";
$result = mysql_query($sql);

 

I then use a while loop to grab this data and display it on my page:

 

 <?php
		//$i = 0;
		$prev = 0;
		$first = true;
		while ($rs = mysql_fetch_array($result))	{


	?>

        
		<tr <?php if (is_int($i/2)) print "bgcolor=\"#A0D6F5\""; ?>>
              <td height="25" style="padding-left:5px; padding-right:2px;"><div class="style1 style8">
                <?= ucwords($rs["Log_Date"]); ?>
              </div></td>
		  <td height="25" align="center" style="padding-left:5px; padding-right:2px;"><div class="style1 style8">
		    <?= ucwords($rs["Channel"]); ?></div></td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><span class="style8">
                <?= ucwords($rs["Zone_Code"]); ?>
              </span> </td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
                <?= ucwords($rs["Device_Code"]); ?>
              </span> </div></td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
                <? 
			if ($rs2['Channel'] == '1') {
  				$cur = $rs["Log_Reading"] * $rs3['Chan1_Pulse_Weight']; }

			else if ($rs2['Channel'] == '2') {	
  				$cur =  $rs["Log_Reading"] * $rs3['Chan2_Pulse_Weight']; }

			else if ($rs2['Channel'] == '3') {	
  				$cur =  $rs["Log_Reading"] * $rs3['Chan3_Pulse_Weight']; }

			else if ($rs2['Channel'] == '4') {	
  				$cur =  $rs["Log_Reading"] * $rs3['Chan4_Pulse_Weight']; }


				echo $cur;
			?>
              </span> </div></td>
              <td align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
		  <?php 
		  
		  
		  if ($first == true) {
			  $first = false;
			  echo "----" ;  
		  } else {
			  
			  echo sprintf("%.2f", $cur - $prev); 
		  }
		  
		  $prev = $cur; 
		  ?>
              </span></div></td>
            </tr>
            <?php $i++; } ?>

 

This is how the data is displayed on the page:

 

table_data.jpg

 

 

 

My question, how do I display this data from my while loop in graph format?

 

 

 

Thanks!

 

Karen

 

 

Link to comment
Share on other sites

Hey everyone,

 

I have a SQL query that pulls data from my database:

 

$sql = "SELECT Log_Date, Log_Reading, Channel, Device_Code, Zone_Code FROM datalog
		WHERE Zone_Code = '".$_REQUEST['zone']."'
		AND Log_Date BETWEEN '".$_REQUEST['day_from']."' AND '".$_REQUEST['day_to']."'
		AND (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan1_Reference='".$_REQUEST['reference']."') AND Channel=1)   
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan2_Reference='".$_REQUEST['reference']."') AND Channel=2)  
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan3_Reference='".$_REQUEST['reference']."') AND Channel=3)  
		OR  (Device_Code IN (SELECT Device_Code FROM devices WHERE Chan4_Reference='".$_REQUEST['reference']."') AND Channel=4) 
		ORDER BY Channel, Log_Date";
$result = mysql_query($sql);

 

I then use a while loop to grab this data and display it on my page:

 

 <?php
		//$i = 0;
		$prev = 0;
		$first = true;
		while ($rs = mysql_fetch_array($result))	{


	?>

        
		<tr <?php if (is_int($i/2)) print "bgcolor=\"#A0D6F5\""; ?>>
              <td height="25" style="padding-left:5px; padding-right:2px;"><div class="style1 style8">
                <?= ucwords($rs["Log_Date"]); ?>
              </div></td>
		  <td height="25" align="center" style="padding-left:5px; padding-right:2px;"><div class="style1 style8">
		    <?= ucwords($rs["Channel"]); ?></div></td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><span class="style8">
                <?= ucwords($rs["Zone_Code"]); ?>
              </span> </td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
                <?= ucwords($rs["Device_Code"]); ?>
              </span> </div></td>
              <td height="25" align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
                <? 
			if ($rs2['Channel'] == '1') {
  				$cur = $rs["Log_Reading"] * $rs3['Chan1_Pulse_Weight']; }

			else if ($rs2['Channel'] == '2') {	
  				$cur =  $rs["Log_Reading"] * $rs3['Chan2_Pulse_Weight']; }

			else if ($rs2['Channel'] == '3') {	
  				$cur =  $rs["Log_Reading"] * $rs3['Chan3_Pulse_Weight']; }

			else if ($rs2['Channel'] == '4') {	
  				$cur =  $rs["Log_Reading"] * $rs3['Chan4_Pulse_Weight']; }


				echo $cur;
			?>
              </span> </div></td>
              <td align="center" class="style1" style="padding-left:5px; padding-right:2px;"><div><span class="style8">
		  <?php 
		  
		  
		  if ($first == true) {
			  $first = false;
			  echo "----" ;  
		  } else {
			  
			  echo sprintf("%.2f", $cur - $prev); 
		  }
		  
		  $prev = $cur; 
		  ?>
              </span></div></td>
            </tr>
            <?php $i++; } ?>

 

This is how the data is displayed on the page:

 

table_data.jpg

 

 

 

My question, how do I display this data from my while loop in graph format?

 

 

 

Thanks!

 

Karen

 

one method:

 

You use a tiny bit of math (percents) and php to parse the img width to your liking and you end up with graph bars.

 

First you find your max value in your table (or in the data set you're going to display on this page).  If the data is from a table, you can just make a quick query to find the max using MAX(YourDataThatYouArePullingOut) in the query to get the max value.

 

Then you make a 1x10 image of a solid color (the second dimension being the height/thickness of the graph bar, so this one would be 10 pixels high).

 

if you wanted some pre-made pretty graphs, there are tons of php graphs that use the gd toolkit to do some really impressive graphs, should you choose the premade route =)

 


<?php

$Max = '300';               //our pretend max value
$CurrentValue = '150';   //our pretend current value

$Width = ( ( $CurrentValue / $Max ) * 800 );  //with a current value of 150, the current bar's width would be 400 (50% of 800)

echo '<img src="bar.gif" border="0" height="10" width="' . $Width . '">';     //this echos the image out, stretched to the right width

//use this kind of code inside a loop to get an entire graph
//you can even have a horizonal graph where the bars grow
//from bottom of a table cell upwards.
//to do this just make sure you throw this
//into your stylesheet:  td img { vertical-align: bottom; }


 

 

 

 

 

 

Link to comment
Share on other sites

Thanks so much for your reply. Sorry if I'm being very blonde but I don't quite understand all of this. I tried using jpGraph and placed it inside my loop but it returns an error that the headers can't be resent and the graph itself is jibberish, just a bunch of funny characters.

 

How do I get the graph code to read the values that's being display rather than doing a seperate query?

 

 

 

Thanks!

 

Karen

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.