Hej Jacob,
jeg var lige i legestuen!!, prøv at kigge på denne code jeg tror det kommer tæt på dit ønske
- CREATE TABLE IF NOT EXISTS `turnover` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `userid` int(11) NOT NULL,
- `measured_date` date NOT NULL,
- `purchased` float NOT NULL,
- `sold` float NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_danish_ci AUTO_INCREMENT=8 ;
-
- INSERT INTO `turnover` (`id`, `userid`, `measured_date`, `purchased`, `sold`) VALUES
- (1, 1, '2012-07-02', 5000, 2000),
- (2, 1, '2012-07-04', 1000, 3000),
- (3, 2, '2012-07-03', 8000, 900),
- (4, 2, '2012-07-04', 800, 100),
- (5, 1, '2012-07-09', 5600, 6000),
- (6, 1, '2012-07-10', 7850, 9000),
- (7, 1, '2012-07-24', 900, 100);
- <?php
- function MySqlIConnOOP(){
- $conn = new mysqli("localhost", "root", "", "test");
- /* check connection */
- if (!$conn) {
- echo 'Der opsod en fejl.';
- exit();
- }
- mysqli_set_charset($conn, "utf8");
- return $conn;
- }
- define ("ONEWEEK", 604800); // (60*60*24*7)
-
- $conn=MySqlIConnOOP();
- $ar=array();
-
- if (isset($_GET['userid']) ){
- $id=$_GET['userid'];
- $week = $_GET['week'];
- $year = date("Y");
-
- $strweek = ($week<10)? "0$week" : "$week";
- // nb week skal være 2 cifret
- $fd=strtotime($year."W".$strweek);
- $firstday = date("Y-m-d",$fd);
- $lastday = date("Y-m-d H:i:s",$fd+(4*ONEWEEK) );
-
- $sql = "select sum(purchased) as sum_purchased, sum(sold) as sum_sold, WEEK(measured_date) as weekno from turnover where userid=$id and measured_date between '$firstday' and '$lastday' GROUP BY weekno"; // YES det virker fanme
- $rs = $conn->query($sql);
- while ($obj = $rs->fetch_object()) {
- $ar[]=array("week ".$obj->weekno,(float)$obj->sum_purchased,(float)$obj->sum_sold);
- }
- if(count($ar)==0){
- $ar[]=array("week ".$week,0.0,0.0);
- }
- if(count($ar)==1) $ar[] = $ar[0]; // der skal 2 målinger til for at få en strig,
- }
- else{
- $ar[]=array(" ",0.0,0.0);
- $ar[]=array(" ",0.0,0.0);
- }
- $jsondata = json_encode($ar);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
- <title></title>
- <script type="text/javascript" src="https://www.google.com/jsapi"></script>
- <script type="text/javascript">
-
- google.load("visualization", "1", {packages:["corechart"]});
- google.setOnLoadCallback(drawChart);
- function drawChart() {
- var values = <?php echo $jsondata; ?>;
- var data = new google.visualization.DataTable();
- data.addColumn('string', 'uge');
- data.addColumn('number', 'købt');
- data.addColumn('number', 'solgt');
-
- for (var k=0, lng=values.length; k<lng;k++){
- data.addRow(values[k]);
- }
-
- var options = {
- title: 'Company Performance'
- };
-
- var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
- chart.draw(data, options);
- }
- </script>
- </head>
- <body>
- <div id="chart_div" style="width: 900px; height: 500px;"></div>
- <form name="testform" action ="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
- <input type="text" name="userid" value ="1"><br>
- <input type="text" name="week" value ="27"><br>
- <input type="submit" name="submit" value="Submit Form"><br>
- </form>
- </body>
- </html>
coden kan optimeres, der mangler validering af input, og man bør anvende AJAX istedet for at genlæse hele siden, men jeg ville ligge coden tæt op af den code du arbejder ud fra.
hvis du ikke gider kopiere coden ligger den her
http://test.olsensweb.dk/jacob/jacob.zip