<?
   require 'dbconnect.inc';
   //get today's date
   $today=getdate();
   //get unix stamp from beginning of today ie 00:00:00 date
   $today_start=mktime(0,0,0,$today['mon']*1.0,$today['mday']*1.0,$today['year']*1.0);
   echo ($today_start);
       //get any dates that moves took place on before beginning of today
       $days = mysql_query(
                "SELECT DISTINCT(DATE_FORMAT(MovedAt,'%Y-%m-%d')) As days ".
                " FROM tblMoves ".
                " WHERE UNIX_TIMESTAMP(tblMoves.MovedAt) < $today_start");

    if (!$days) {
      echo("<P>Error performing query: " .
           mysql_error() . "</P>");
      exit();
    }
             //echo("<p><u>Days</u></p>");
             while ( $row = mysql_fetch_array($days) ) {
             $day = $row["days"];

             //echo("<P>$day</P>");
             
             //check to make sure that day isn't already archived
             $check= mysql_query(
                "SELECT Count(*) As already_archived ".
                " FROM tblArchive ".
                " WHERE fldArchiveDate = '$day' ");
                if(!$check){
                    echo("<P>Error performing query: " .
                     mysql_error() . "</P>");
                     exit();
                }
                while ( $row = mysql_fetch_array($check) ) {
                      $already_archived=$row["already_archived"];
                      if($already_archived<1){

                           //count total number of moves for that day
                           $move_count = mysql_query(
                           "SELECT Count(*) As move_count ".
                           " FROM tblMoves ".
                           " WHERE DATE(MovedAt) = '$day' ");

                           if (!$move_count) {
                              echo("<P>Error performing query: " .
                              mysql_error() . "</P>");
                              exit();
                           }
                           //echo("<p><u>Latest</u></p>");
                           while ( $row = mysql_fetch_array($move_count) ) {
                                 $total_moves = $row["move_count"];

                                 //echo("<P>$total_moves for $day</P>");

                          }
                          //set up archive array
                          $archive=array(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0);

                          //get percentage of moves for each symbol on that day
                          $symbol_count= mysql_query(
                               "SELECT Symbol,((Count(Symbol)/$total_moves)*100) As Percent ".
                               " FROM tblMoves ".
                               " WHERE DATE(MovedAt) = '$day' ".
                               " GROUP BY Symbol");

                          if (!$symbol_count) {
                             echo("<P>Error performing query: " .
                             mysql_error() . "</P>");
                             exit();
                          }
                          //echo("<p><u>Percents</u></p>");
                          while ( $row = mysql_fetch_array($symbol_count) ) {
                                $symbol = $row["Symbol"];
                                $percent = $row["Percent"];

                                //echo("<P>$symbol - $percent%</P>");
                      
                                //set up loop to insert percents
                                for($counter=0;$counter<9;$counter++){
                                   if($symbol==$counter){
                                      //echo(" inserted $percent ");
                                      $archive[$counter]=$percent;
                                   }

                                   //echo("</p>");
                                }

                         }
                         //print_r($archive);
               
                         //archive percentages
                         $add_archive="INSERT INTO tblArchive (fldArchiveDate, fldSymbol0, fldSymbol1, ";
                         $add_archive.="fldSymbol2, fldSymbol3, fldSymbol4, fldSymbol5, fldSymbol6, ";
                         $add_archive.="fldSymbol7, fldSymbol8) ";
                         $add_archive.= " VALUES ('$day', $archive[0], $archive[1], $archive[2], $archive[3], ";
                         $add_archive.="$archive[4], $archive[5], $archive[6], $archive[7],$archive[8])";

                         if (mysql_query($add_archive))
                         {
                            //echo "Archived successfully";
                         }
                         else
                         {
                            echo("<P>Error archiving: " .
                            mysql_error() . "</P>");
                         }
               
               
                         /*$show_archive = mysql_query(
                                       "SELECT * ".
                                       " FROM tblArchive ");


                         if (!$show_archive) {
                            echo("<P>Error performing query: " .
                            mysql_error() . "</P>");
                            exit();
                         }
                         echo("<p><u>Archive</u></p>");
                         while ( $row = mysql_fetch_array($show_archive) ) {
                               $a_date = $row["fldArchiveDate"];
                               $s2 = $row["fldSymbol2"];
                               $s4 = $row["fldSymbol4"];
                               //$ac = $row["archive_count"];
                               echo("<P>$a_date - $s2 - $s4</P>");

                         } */
                      }
                }
             }
?>

