title: create graph in php and mysql
body: <p>I have a database like the attached image.<img src="https://i.stack.imgur.com/yLxxN.png" alt="enter image description here"></p>
  
  <p>By that table I need to draw a graph like this<img src="https://i.stack.imgur.com/lG3Gv.png" alt="enter image description here"></p>
  
  <p>How to do that? I have tried with the following code:</p>
  
  <pre><code>function get_number($hour){
  $sql = "SELECT count(POPUP)+count(`DETAIL`)+count(`DETAIL`)+count(`CALL`)+count(`MESSAGE`)+count(`ROUTE`)+count(`SHARE`)+count(`MAP`)+count(`INFO`)+ count(`WEBSITE`) as cp
  FROM campaign_logs WHERE EXTRACT(HOUR FROM logDate)=".$hour;    
      $res = mysql_fetch_array(mysql_query($sql));
      return $res['cp'];
  }
  
  $sql = "SELECT count(POPUP)+count(`DETAIL`)+count(`DETAIL`)+count(`CALL`)+count(`MESSAGE`)+count(`ROUTE`)+count(`SHARE`)+count(`MAP`)+count(`INFO`)+ count(`WEBSITE`) as cp,EXTRACT(HOUR FROM logDate) as h
  FROM campaign_logs GROUP BY EXTRACT(HOUR FROM logDate)";
  $res = mysql_query($sql);
  
  $values=array(
  "1" =&gt; get_number(1),
  "2" =&gt; get_number(2),
  "3" =&gt; get_number(3),
  "4" =&gt; get_number(4),
  "5" =&gt; get_number(5),
  "6" =&gt; get_number(6),
  "7" =&gt; get_number(7),
  "8" =&gt; get_number(8),
  "9" =&gt; get_number(9),
  "10" =&gt; get_number(10),
  "11" =&gt; get_number(11),
  "12" =&gt; get_number(12),
  "13" =&gt; get_number(13),
  "14" =&gt; get_number(14),
  "15" =&gt; get_number(15),
  "16" =&gt; get_number(16),
  "17" =&gt; get_number(17),
  "18" =&gt; get_number(18),
  "19" =&gt; get_number(19),
  "20" =&gt; get_number(20),
  "21" =&gt; get_number(21),
  "22" =&gt; get_number(22),
  "23" =&gt; get_number(23),
  "24" =&gt; get_number(24)
  );
  
  
  $img_width=600;
  $img_height=400; 
  $margins=20;
  $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); 
  $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); 
  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=max($values);
  $ratio= $graph_height/$max_value; 
  $horizontal_lines=20;
  $horizontal_gap=$graph_height/$horizontal_lines;
  for($i=1;$i&lt;=$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);
  }
  for($i=0;$i&lt; $total_bars; $i++){
  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;
  imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
  imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
  imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
  } 
  
  header("Content-type:image/png");
  imagepng($img); 
  </code></pre>
  
  <p>But it is something different. It is calculating not separately.
  Please help me.</p>