php 通过php从mysql检索数据以创建浮图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1312302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:59:39  来源:igfitidea点击:

Retrieve data from mysql by php to create flot graph

phpflot

提问by Sarah

Hi i am trying to retrieve data from mysql database to create flot graph can anyone walk me through this procedure or give me an idea of what to do thanks

嗨,我正在尝试从 mysql 数据库中检索数据以创建浮图图,任何人都可以引导我完成此过程或让我知道该怎么做,谢谢

回答by Tom Haigh

You probably want something like this. I haven't used flot but I looked at the example here.

你可能想要这样的东西。我没有使用过 flot,但我在这里查看了示例。

<?php
//create array of pairs of x and y values
$dataset1 = array();
while ($row = mysql_fetch_assoc()) { //or whatever
    $dataset1[] = array( $row['xvalue'], $row['yvalue'] );
}
?>

<script type="text/javascript">
    //put array into javascript variable
    var dataset1 = <?php echo json_encode($dataset1); ?>;

    //plot
    $(function () {
         $.plot($("#placeholder"), [ dataset1 ]);
    });
</script>

回答by John M

Adding upon the example from @Tom Haigh:

添加来自@Tom Haigh的示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples</title>
    <link href="layout.css" rel="stylesheet" type="text/css">
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
    <script language="javascript" type="text/javascript" src="../jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
 </head>
    <body>
    <h1>Flot Examples</h1>

    <div id="placeholder" style="width:600px;height:300px;"></div>

<?php

$server = "localhost";
    $user="user";
    $password="password";  
    $database = "some_database";

    $connection = mysql_connect($server,$user,$password);
    $db = mysql_select_db($database,$connection);

query = "SELECT x_axis_values, y_axis_values FROM some_table";
    $result = mysql_query($query);        

    while($row = mysql_fetch_assoc($result))
    {
        $dataset1[] = array($row['x_axis_value'],$row['y_axis_value']);
    }

?>


<script type="text/javascript">
$(function () {
    var dataset1 = <?php echo json_encode($dataset1); ?>;

    $.plot($("#placeholder"), [ dataset1 ]);
});
</script>

 </body>
</html>

回答by Skylex

as @Tom Haigh say work well, but you need to add another code to work well, I was using the example, but I discover in the source code it add to the result quotes " so to avoid this just add the: intval to the array, example:

正如@Tom Haigh 所说的那样工作良好,但您需要添加另一个代码才能正常工作,我正在使用该示例,但我在源代码中发现它添加到结果引号中“所以为了避免这种情况,只需将:intval 添加到数组,示例:

<?php
$query = "SELECT valx, valy FROM chart";
    $result = mysql_query($query);        

    while($row = mysql_fetch_assoc($result))
    {
        $d2[] = array (intval($row['valx']),intval($row['valy']));
    }
?>

回答by Steven Surowiec

This depends largely on your environment and requirements. There's lots of free tools out there you can use. One example is Flotthat lets you use jQuery to build graphs. There's link to documentation on the Google Code page.

这在很大程度上取决于您的环境和要求。有很多免费工具可供您使用。一个例子是Flot,它允许您使用 jQuery 来构建图形。Google 代码页面上有指向文档的链接。