timmah1 Posted March 2, 2012 Share Posted March 2, 2012 I'm trying to get a bar graph from total number of hit on a month to month basis. Everything works out find, except the bar graph is only showing 1 month, not everything that's being pulled out of the db. Can anybody look and see what I'm doing wrong? $userid = '44'; $t_month = date("Y-m-d h:i:s"); $y_month = date("Y-m-d h:i:s", strtotime("-1 Year")); //Grabs Unique Visitors to App Page $query = "SELECT CONCAT_WS(' ', YEAR(date), MONTHNAME(date)) AS RptDate, COUNT(DISTINCT referrer) 'referrer' FROM app_analytics WHERE appid = $userid AND date BETWEEN '" .$y_month."' AND '" .$t_month."' GROUP BY CONCAT_WS(' ', YEAR(date), MONTHNAME(date)) ORDER BY YEAR(date), MONTH(date)"; $result = mysql_query($query) or die(mysql_error()); //$totals = array(); while($row = mysql_fetch_array( $result )){ $months = date("M", strtotime($row['RptDate'])); $ip = $row['referrer']; $values = array("$months" => $ip); $img_width=450; $img_height=300; $margins=20; # ---- Find the size of graph by substracting the size of borders $graph_width=$img_width - $margins * 2; $graph_height=$img_height - $margins * 2; $img=imagecreate($img_width,$img_height); $bar_width=20; $total_bars=count($values); $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); # ------- Define Colors ---------------- $bar_color=imagecolorallocate($img,0,64,128); $background_color=imagecolorallocate($img,240,240,255); $border_color=imagecolorallocate($img,200,200,200); $line_color=imagecolorallocate($img,220,220,220); # ------ Create the border around the graph ------ imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color); imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); # ------- Max value is required to adjust the scale ------- $max_value=max($values); $ratio= $graph_height/$max_value; # -------- Create scale and draw horizontal lines -------- $horizontal_lines=20; $horizontal_gap=$graph_height/$horizontal_lines; for($i=1;$i<=$horizontal_lines;$i++){ $y=$img_height - $margins - $horizontal_gap * $i ; imageline($img,$margins,$y,$img_width-$margins,$y,$line_color); $v=intval($horizontal_gap * $i /$ratio); imagestring($img,0,5,$y-5,$v,$bar_color); } # ----------- Draw the bars here ------ for($i=0;$i< $total_bars; $i++){ # ------ Extract key and value pair from the current pointer position list($key,$value)=each($values); $x1= $margins + $gap + $i * ($gap+$bar_width) ; $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($value * $ratio) ; $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color); imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } header("Content-type:image/png"); imagepng($img); } Thanks in advance Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/ Share on other sites More sharing options...
btherl Posted March 2, 2012 Share Posted March 2, 2012 It looks like you are running all the graph drawing code for every row returned from the database. Is that the way it is supposed to be working? Or do you want to read all the data from the database first and then draw the graph in one go? Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1323052 Share on other sites More sharing options...
timmah1 Posted March 2, 2012 Author Share Posted March 2, 2012 I'm trying to get it to grab all the info, then draw the bar graph Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1323080 Share on other sites More sharing options...
timmah1 Posted March 2, 2012 Author Share Posted March 2, 2012 Bump Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1323195 Share on other sites More sharing options...
btherl Posted March 3, 2012 Share Posted March 3, 2012 Ok, the first thing to do is use consistent indentation. The "}" which ends the while loop needs to be directly under the "w" from the start of the word "while". That will make it clear where the end of the loop is. If the end of the loop is a long way away, add a comment after the "}" to tell you which loop it's from. I can't actually tell from your code where the while loop ends, but I suspect it includes all your graph drawing code. Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1323449 Share on other sites More sharing options...
timmah1 Posted March 5, 2012 Author Share Posted March 5, 2012 ok, here is the code, better formatted to understand. It is still only showing 1 month in the graph <?php include ($_SERVER['DOCUMENT_ROOT'] . "/inc/config.db.php"); $userid = '44'; #--will be pulled from login after testing $t_month = date("Y-m-d h:i:s"); $y_month = date("Y-m-d h:i:s", strtotime("-1 Year")); //Grabs Unique Visitors to App Page $query = "SELECT CONCAT_WS(' ', YEAR(date), MONTHNAME(date)) AS RptDate, COUNT(DISTINCT referrer) 'referrer' FROM app_analytics WHERE appid = $userid AND date BETWEEN '" .$y_month."' AND '" .$t_month."' GROUP BY CONCAT_WS(' ', YEAR(date), MONTHNAME(date)) ORDER BY YEAR(date), MONTH(date)"; $result = mysql_query($query) or die(mysql_error()); //$totals = array(); while($row = mysql_fetch_array( $result )){ $months = date("M", strtotime($row['RptDate'])); $ip = $row['referrer']; $values = array("$months" => $ip); } #-- end while loop $img_width=450; $img_height=300; $margins=20; # ---- Find the size of graph by substracting the size of borders $graph_width=$img_width - $margins * 2; $graph_height=$img_height - $margins * 2; $img=imagecreate($img_width,$img_height); $bar_width=20; $total_bars=count($values); $gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1); # ------- Define Colors ---------------- $bar_color=imagecolorallocate($img,0,64,128); $background_color=imagecolorallocate($img,240,240,255); $border_color=imagecolorallocate($img,200,200,200); $line_color=imagecolorallocate($img,220,220,220); # ------ Create the border around the graph ------ imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color); imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color); # ------- Max value is required to adjust the scale ------- $max_value=max($values); $ratio= $graph_height/$max_value; # -------- Create scale and draw horizontal lines -------- $horizontal_lines=20; $horizontal_gap=$graph_height/$horizontal_lines; for($i=1;$i<=$horizontal_lines;$i++){ $y=$img_height - $margins - $horizontal_gap * $i ; imageline($img,$margins,$y,$img_width-$margins,$y,$line_color); $v=intval($horizontal_gap * $i /$ratio); imagestring($img,0,5,$y-5,$v,$bar_color); } # ----------- Draw the bars here ------ for($i=0;$i< $total_bars; $i++){ # ------ Extract key and value pair from the current pointer position list($key,$value)=each($values); $x1= $margins + $gap + $i * ($gap+$bar_width) ; $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($value * $ratio) ; $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color); imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } header("Content-type:image/png"); imagepng($img); ?> Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324211 Share on other sites More sharing options...
btherl Posted March 5, 2012 Share Posted March 5, 2012 Ok that's an improvement on the previous version. Are you familiar with arrays? What you need to do is add one item into the $values array each time around the while loop. For example, it might look like this: while($row = mysql_fetch_array( $result )){ $months = date("M", strtotime($row['RptDate'])); $ip = $row['referrer']; $values[] = array("$months" => $ip); } #-- end while loop At the end of this you can insert the following line to debug: print "<pre>"; var_dump($values); exit; If it all looks correct, remove the line and continue coding. Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324299 Share on other sites More sharing options...
timmah1 Posted March 6, 2012 Author Share Posted March 6, 2012 Thank you. No, I'm not very familiar with array's. I'm trying to learn more about them The code puts out what's it suppose too array(10) { [0]=> array(1) { ["Jun"]=> string(2) "61" } [1]=> array(1) { ["Jul"]=> string(4) "1156" } [2]=> array(1) { ["Aug"]=> string(3) "430" } [3]=> array(1) { ["Sep"]=> string(3) "205" } [4]=> array(1) { ["Oct"]=> string(3) "151" } [5]=> array(1) { ["Nov"]=> string(3) "165" } [6]=> array(1) { ["Dec"]=> string(2) "96" } [7]=> array(1) { ["Jan"]=> string(3) "175" } [8]=> array(1) { ["Feb"]=> string(3) "148" } [9]=> array(1) { ["Mar"]=> string(2) "30" } } Now I'm getting an error, and to be honest, I have no idea how to fix it. The error is this Fatal error: Unsupported operand types in /home/local/public_html/members-new/checkip1.php on line 80 And line 80 is this $y1=$margins +$graph_height- intval($value * $ratio) ; and that's inside this for loop for($i=0;$i< $total_bars; $i++){ # ------ Extract key and value pair from the current pointer position list($key,$value)=each($values); $x1= $margins + $gap + $i * ($gap+$bar_width) ; $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($value * $ratio) ; $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color); imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324331 Share on other sites More sharing options...
btherl Posted March 6, 2012 Share Posted March 6, 2012 That line looks sensible to me. Can you insert this line just before it please: print "Margins: " ; var_dump($margins); print "graph_height: " ; var_dump($graph_height); print "value: " ; var_dump($value) ; print "ratio: " ; var_dump($ratio); With any luck that will show you that one of those variables is not what it should be. Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324354 Share on other sites More sharing options...
timmah1 Posted March 6, 2012 Author Share Posted March 6, 2012 this is what it outputs Margins: NULL graph_height: NULL value: NULL ratio: NULL Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324422 Share on other sites More sharing options...
timmah1 Posted March 6, 2012 Author Share Posted March 6, 2012 Sorry This is what's output Margins: int(20) graph_height: int(260) value: NULL ratio: float(4.26229508197) Fatal error: Unsupported operand types in /home/local/public_html/members-new/checkip1.php on line 80 Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324423 Share on other sites More sharing options...
timmah1 Posted March 6, 2012 Author Share Posted March 6, 2012 Taking this code print "<pre>Margins: " ; var_dump($margins); print "graph_height: " ; var_dump($graph_height); print "value: " ; var_dump($values) ; print "ratio: " ; var_dump($ratio); and running it before the error, this is what prints out Margins: int(20) graph_height: int(260) value: array(10) { [0]=> array(1) { ["Jun"]=> string(2) "61" } [1]=> array(1) { ["Jul"]=> string(4) "1156" } [2]=> array(1) { ["Aug"]=> string(3) "430" } [3]=> array(1) { ["Sep"]=> string(3) "205" } [4]=> array(1) { ["Oct"]=> string(3) "151" } [5]=> array(1) { ["Nov"]=> string(3) "165" } [6]=> array(1) { ["Dec"]=> string(2) "96" } [7]=> array(1) { ["Jan"]=> string(3) "175" } [8]=> array(1) { ["Feb"]=> string(3) "148" } [9]=> array(1) { ["Mar"]=> string(2) "36" } } ratio: float(4.26229508197) Fatal error: Unsupported operand types in /home/local/public_html/members-new/checkip1.php on line 80 and the code producing the error for($i=0;$i< $total_bars; $i++){ # ------ Extract key and value pair from the current pointer position list($key,$value)=each($values); $x1= $margins + $gap + $i * ($gap+$bar_width) ; $x2= $x1 + $bar_width; $y1=$margins +$graph_height- intval($values * $ratio) ; #<--LINE 80 $y2=$img_height-$margins; imagestring($img,0,$x1+3,$y1-10,$value,$bar_color); imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color); imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color); } Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324568 Share on other sites More sharing options...
timmah1 Posted March 6, 2012 Author Share Posted March 6, 2012 Checking some other things, I'm getting an error here as well Fatal error: Unsupported operand types in /home/local/public_html/members-new/checkip1.php on line 59 $max_value=max($values); $ratio= $graph_height/$max_value; Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324570 Share on other sites More sharing options...
btherl Posted March 6, 2012 Share Posted March 6, 2012 The first time you posted line 80 it said intval($value * $ratio), but the second time you posted it it said intval($values * $ratio). It needs to be $value, not $values. Whenever you have the error about unsupported operand types, use var_dump() to print out the values you are using in that line. You should find that the values aren't what you expect them to be. Link to comment https://forums.phpfreaks.com/topic/258084-graph-only-showing-1-month/#findComment-1324592 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.