title: Count record each day of a month from mysql into html table
body: <p>I'd like to put all mySql results in a html table. This is mySql:</p>
  
  <pre><code>SELECT date(vwr_date) AS mon, date(vwr_date) AS date, count(vwr_cid) AS views 
  FROM car_viewer 
  WHERE Year(vwr_date)='2012' AND vwr_tid='18' 
  GROUP BY date 
  ORDER BY date DESC
  </code></pre>
  
  <p>From the query above, it results like this:</p>
  
  <pre><code>date        views
  2012-10-23  14
  2012-10-22  339
  2012-10-21  305
  2012-10-20  354
  ....
  2012-10-01  291
  2012-09-30  246
  2012-09-29  297
  ...
  2012-09-01  281
  </code></pre>
  
  <p>The result from mysql is very good. And now this is a problem. I tried many times and different ways to put those views into  according to each date from car_viewer.vwr_date. So I wrote like this:</p>
  
  <pre><code>&lt;table border="0" cellpadding="3" cellspacing="1" width="100%"&gt;
          &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Mon Year / Date&lt;/th&gt;
              &lt;?
              for($i=1;$i&lt;32;$i++){
                  echo "&lt;th&gt;$i&lt;/th&gt;";
              }
              ?&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;?
          $_thisYear=date("Y");
          $sql_nViewer="select date(vwr_date) as mon, date(vwr_date) as date, count(vwr_cid) as views from car_viewer where Year(vwr_date)='$_thisYear' and vwr_tid='$_SESSION[admin_id]' GROUP BY date ORDER BY date desc";
          echo $sql_nViewer;
          $result_nViewer=mysql_db_query($dbname,$sql_nViewer);
          $rec_nViewer=mysql_fetch_array($result_nViewer);
          $_monYear=date("M Y",strtotime($rec_nViewer[mon]));
          $_monYear2=date("Y-n",strtotime($rec_nViewer[mon]));
  
              echo "&lt;tr&gt;&lt;td&gt;$_monYear&lt;/td&gt;";//month name
          $day=1;
          while($rec_nViewer=mysql_fetch_assoc($result_nViewer)){
              $_nViewerDBdate=$rec_nViewer['date'];
              $_nViewerDBviews=$rec_nViewer['views'];
              echo "
              &lt;td&gt;$_nViewerDBviews&lt;/td&gt;
              ";$day++;
              if($day==$_daysInMon+1){
                  echo "&lt;/tr&gt;";
              }
          }
          ?&gt;
          &lt;/tbody&gt;
          &lt;/table&gt;
  </code></pre>
  
  <p>The result of codes above is weird - not as what I want. I need the data to be displayed like this:
  <img src="https://i.stack.imgur.com/sS6P6.png" alt="expected table"></p>
  
  <p>So please suggest what should I do to make it as I expected. Regards</p>