2012-02-25 29 views
3

我試圖用Flot,html,PHP和MySql查詢繪製多個圖表,但我被卡住了,因爲我無法找到一種方法來繪製多個圖表flots在同一個html頁面中。爲簡單起見 在數據庫(test_db3)圖像下列字段:用Flot,html,PHP和MySql查詢繪製多個圖表

  • 表1(USER_NAME,mail_sent,時間)
  • 表2(USER_NAME2,mail_received,TIME2)

這兩個表的不能修改,我不能將user_name2添加到table1等等。 table1中存儲基於表2中發送 的時間發送郵件的值存儲基於接收

時此代碼之前接收郵件的價值,我已經測試存儲在數據該數據庫與之前編寫的另一代碼相比,它只能繪製一個用戶的2個圖表,但數據和圖表正確繪製。 現在我試圖爲數據庫的所有用戶繪製2個圖表,我卡住了! 如有必要,我可以發佈從數據庫中爲單個用戶提取數據的第一個代碼。 如果有人有任何建議......謝謝!

<html> 
<script language="javascript" type="text/javascript" src="js/jquery.js"></script> 
<script language="javascript" type="text/javascript" src="js/jquery.flot.js"></script> 
<?php 
/* 
connection to the database 
*/ 
    $server = "localhost"; 
    $user="xxxxxx"; 
    $password=" xxxxxx "; 
    $database = "test_db3"; 
    $connection = mysql_connect($server,$user,$password) or die (mysql_error());  
    $db = mysql_select_db($database,$connection) or die (mysql_error()); 

    //The first Sql query is searching for DISTINCT users in the DB 
    $data = mysql_query("SELECT DISTINCT user_name FROM table1 JOIN table2 ON user_name=user_name2") or die(mysql_error()); 

    while($info = mysql_fetch_array($data)) 
     { 
      $user = $info['user_name']; //It’s the name of user analyzed at the moment 
      /* 
      This query extract the first ten more recents values (order by time DESC) 
      The data retrieved by the query are used to paint the 1° chart for emails sent by the user 
      but I don't know how to do it recursively 
      */ 
      $query = "SELECT user_name,mail_sent,time FROM table1 WHERE user_name='$user' ORDER BY time DESC LIMIT 0,10"; 
      $result = mysql_query($query); 
      while($row = mysql_fetch_assoc($result)) 
       { 
        $row['time']=$row['time']*1000; //The time is in millisecond I need to multiply for 1000 in order to obtain the seconds 
        //the 'time' row is the x-axis , the 'mail_send' row is the y-axsis 
        $dataset1[] = array($row['time'],$row['mail_sent']); //It contains the time value and the numbers of email sent from the user 
       } 

      /* 
      This query extract the first ten more recents values (order by time2 DESC) 
      The data retrieved by the query are used to paint the 2° chart for emails received by the user 
      but I don't know how to do it recursively 
      */ 
      $query2 = "SELECT user_name2,mail_received ,time2 FROM table2 WHERE user_name2='$user' ORDER BY time2 DESC LIMIT 0,10"; 
      $result2 = mysql_query($query2); 
      while($row2 = mysql_fetch_assoc($result2)) 
       { 
        $row2['time2']=$row2['time2']*1000; //The time is in millisecond I need to multiply for 1000 in order to obtain the seconds 
        //the 'time' row is the x-axis , the 'mail_send' row is the y-axsis 
        $dataset2[] = array($row2['time2'],$row2['mail_received ']); //It contains the time value and the numbers of email received from the user 
       } 
        /* 
        Here I should insert some code in order to draw 2 charts for all the users of the DB, so for example if the DB has 30 Users 
        i need to draw 60 charts, 2 charts for any users 
        -> the 1° charts represents the mail sent from the user 
        -> the 2° charts represents the mail received from the user 
        */ 
     } 
    mysql_close($connection); //Close connection DB 
?> 

<script type="text/javascript"> 
$(function() { 
// setup plot 
    var options = { 
     series: { 
      lines: { show: true }, 
      points: { show: true } 
       }, 
     //the value of min:0 and max:100 are just examples of course 
     yaxis: { min: 0, max: 100 }, 

     xaxis: { 
     mode: "time", 
     minTickSize: [1, "minute"], 

       } 

    }; 

    var dataset1 = <?php echo json_encode($dataset1); ?>; 
    var dataset2 = <?php echo json_encode($dataset2); ?>; 


    //This part is not correct because the palaceholder should have a increment value 
    //placeholder0, placeholder1, placeholder3, placeholder4, ..., placeholderN 
    //And it is necessary to place a <div id="placeholderN" style="width:350px;height:200px;"> </div> in the PHP code for every placeholder generated 
    //or find another solution 

    var plot1 = $.plot($(placeholder0), [ dataset1, dataset2 ], options); //For the 1° charts 
    var plot2 = $.plot($(placeholder1), [ dataset1, dataset2 ], options); //Fot the 2° charts 

    });//End script 
</script> 
</html> 

回答

0

首先,讓user_name2,time2等有點奇怪。爲第二個查詢。這是真的如何在你的數據庫中設置?

無論如何,這裏有一種方法可以從您的PHP循環中生成圖。

echo('<div id="placeholder"></div>'); 
    echo('<script>'); 
    while($info = mysql_fetch_array($data)) 
     { 
      $user = $info['user_name']; //It’s the name of user analyzed at the moment 
      /* 
      This query extract the first ten more recents values (order by time DESC) 
      The data retrieved by the query are used to paint the 1° chart for emails sent by the user 
      but I don't know how to do it recursively 
      */ 
      $query = "SELECT user_name,mail_sent,mail_received,time FROM table1 WHERE user_name='$user' ORDER BY time DESC LIMIT 0,10"; 
      $result = mysql_query($query); 
      $dataset1 = new Array(); 
      while($row = mysql_fetch_assoc($result)) 
       { 
        $row['time']=$row['time']*1000; //The time is in millisecond I need to multiply for 1000 in order to obtain the seconds 
        //the 'time' row is the x-axis , the 'mail_send' row is the y-axsis 
        $dataset1[] = array($row['time'],$row['mail_sent']); //It contains the time value and the numbers of email sent from the user 
       } 
      echo('$.plot($(\'<div style="width:600px;height:300px;"></div>\').appendTo(\'#placeholder\'),'.json_encode($dataset1).',options);\n'); 
     } 
    echo('</script>');