我尝试在D3中创建一个简单的条形图,从数据库中获取数据。其思想是使用parameters和country的选择按钮从数据库中获取年份和值,并以简单的条形图形式显示。我有两个选择按钮和一个提交按钮。其代码如下所示:
<form action ="data.php" method ="post">
<tr>
<th valign="bottom">Select country:</th>
<td >
<select name="country" class="">
<option value= "NPL">Nepal</option>
<option value= "IND">India</option>
</select>
</td>
<th valign="bottom">Select parameters:</th>
<td >
<select name="indices" class="">
<option value= "foodfinal">Vulnerability on food</option>
<option value= "waterfinal">Vulnerability water </option>
</select>
</td>
<td valign="top">
<input type="submit" class="action" />
</td>
</tr>
</form> Foodfinal和waterfinal是名为“气候”的数据库中的表的名称。NPL和IND是国家代码。我的PHP代码( data.php )如下:
<?php
$username = "root";
$password = "";
$host = "localhost";
$database="climate";
$country="AFG";
$country=$_POST["country"];
$indices=$_POST["indices"];
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "SELECT `year`, `values` FROM `$indices` WHERE `countrycode`= '$country'";
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
include("linegraph.html");
mysql_close($server);
?>我的问题是,在使用选择表单之前,linegraph.html一直工作得很好,但这次,它只显示如下所示的data.php输出。
{“年”:“1995年”,“值”:“0.525999”},{“年”:“1996年”,“值”:“0.570037”},{“年”:“1997年”,“值”:“0.563966”},{“年”:“1998年”,“值”:“0.513896”},{“年”:“1999年”,“值”:“0.5346”},{“年”:“2000年”,“值”:“0.552691”},{“年”:“2001年”,“值”:“0.545319”},{“年”:“2004年”,“值”:“0.543972”},{“年”:“2005年”,“值”:“0.543299”},{“年”:“2006年”,“值”:“0.546682”},{“年”:“2007年”,“值”:“0.550066”},{“年”:“2008年”,“值”:“0.549449”},{“年”:“2009年”,“值”:“0.548832”},{“年”:“2010年”,“值”:“0.548215”},{“年”:“2011年”,“值”:“0.547536”},{“年”:“2012年”,“值”:“0.547536”}]
你能帮帮我吗?
发布于 2014-07-13 04:50:26
改变这一点
include("linegraph.html"); To (作为最佳实践,如果需要"include“来显示页面,只需使用required )
require("linegraph.html"); 并在php的顶部添加
ini_set('display_errors', 1); 你可能会发现你的页面丢失了(至少打开错误报告后你的生活会变得更容易)
哦,把这部分也改一改,
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}到这个
$data = array();
while ( false !== ( $row = mysql_fetch_assoc($query))){
$data[] = $row;
}它可能不会解决你的问题(如果没有其他的html页面很难做到),但这是做这种循环的首选方法。
更新:在这里,你不能像现在这样同时传递html和json (我才刚刚明白),你必须选择一个。你的问题是一个结构上的问题,因为你不能同时做这两件事,json数据去哪里了,它只是漂浮在html的顶部吗?对不起,但它不会像那样工作,要么你已经有了html并用返回的json数据填充它,要么你用已经在其中的数据构建html并将其发送回来。
改变这一点
echo json_encode($data);
include("linegraph.html");到(现在你正在发送有效的json /和html,但是你必须在客户端组装它)
ob_start();
include("linegraph.html");
$html = ob_get_clean();
$responce = array( 'data'=>$data, 'html'=>$html );
echo json_encode($responce); 至少你的输出会改变。没有另一个页面,很难说(再次)怎么做。
好的,,,当我要求在底部回显一些东西时,你这样做了吗?
echo json_encode($data);
include("linegraph.html");
mysql_close($server);
echo "works";
?> //id remove this end tag it's not needed and can cause problems if there is space ( line returns ) after it. Because it will make you json which you may or may not need invalid.太棒了。这是..。啊哈
if ( ! $query ) {
echo mysql_error();
die;
}你可以这样做。
if ( ! $query ) {
die ('Query error' . mysql_error());
}https://stackoverflow.com/questions/24716965
复制相似问题