2011-11-14 5 views
0

Donc, je construis une section de statistiques pour un site Web que je prépare pour le travail (demande des gestionnaires). J'ai un code de fonctionnement pour un graphique '24 heures '. Le code graphique j'ai fonctionne parfaitement, mais je vais avoir des problèmes à obtenir le bon code pour MySQL pour retirer les données en fonction des jours plutôt que des heuresCréation de graphiques hebdomadaires et mensuels en utilisant SQL et php pour l'utilisation du site

Code horaire ci-dessous

function get_hourly_graph() { 
    // First lets create a daily graph for experiment then we can build on that 
    date_default_timezone_set('Australia/Sydney'); 
    $current_hour = date('H'); 

    // SQL Query to get data we want 
    $sql = mysql_query("SELECT hour(time_hit) AS hour , count(hour(time_hit)) AS hits FROM hits WHERE time_hit > now() - INTERVAL 1 DAY GROUP BY hour(time_hit);") 
     or die(mysql_error()); 

    // Remove an hour off current time for array and the loop that will put the data into the array 
    $array_hour = $current_hour - 1; 

    // Add a 0 if the timer is under 10. just so this is compatable with the SQL data 
    if ($array_hour < 10) { 
     $array_hour = 0 . $array_hour; 
    } 

    // initialise the array so we can set up the indexes and data in the next couple of loops 
    $hit_data = array(); 

    // Work back the clock 24 hours and create the array indexes 
    for ($i = 0; $i < 24; $i++) { 
     $new_hour = $current_hour; 

     if ($new_hour < 1) { 
      $new_hour = 24; 
      $hit_data[$new_hour] = "0"; 
     } 
     else { 
      $hit_data[$new_hour] = "0"; 
     } 

     $current_hour = $new_hour - 1; 
    } 

    // Loop through the rest of the array and replace null with 0s 
    while ($results = mysql_fetch_array($sql, MYSQL_ASSOC)) { 
     foreach ($hit_data as $hour => $hits) { 
      if ($results['hour'] == $hour) { 
       $hit_data[$hour] = $results['hits']; 
      } 
     } 
    } 

    $hit_data = array_reverse($hit_data, true); 

    // Generate the graph for the 24 hour data 
    generate_graph ($hit_data, '24 Hour Report', 'hourly_graph.png', 'Time (Hours)', 'Hits'); 
} 

la table « hits » est essentiellement 3 champs 'hit_id' 'ip_address' 'timestamp' qui est sous 'AAAA-MM-JJ hh: mm: ss' = "2011-08-04 12:40:34"

Comment puis-je changer le code SQL ci-dessous pour travailler avec des jours au lieu d'heures?

Répondre

0

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <!--Script Reference[1]--> 
 

 
    <script src="https://cdn.zingchart.com/zingchart.min.js"></script> 
 
</head> 
 
<body> 
 
    <!--Chart Placement[2]--> 
 
    <div id="chartDiv"></div> 
 
    <script> 
 
    var chartData = { 
 
     type: "bar", // Specify your chart type here. 
 
     title: { 
 
     text: "My First Chart" // Adds a title to your chart 
 
     }, 
 
     legend: {}, // Creates an interactive legend 
 
     series: [ // Insert your series data here. 
 
      { values: [10, 42, 67, 89,70]}, 
 
     
 
     ] 
 
    }; 
 
    zingchart.render({ // Render Method[3] 
 
     id: "chartDiv", 
 
     data: chartData, 
 
     height: 400, 
 
     width: 400 
 
    }); 
 
    </script> 
 
</body> 
 
</html>

+0

S'il vous plaît expliquer –

Questions connexes