這是我的表結構。
如何使用php和mysql循環數據[]系列
我使用highcharts
這裏是我的代碼:
$(function() {
$('#container_journal').highcharts({
xAxis: {
categories: [
'1995', '1996', '1997', '1988'
]
},
yAxis: {
title: {
text: 'Citations'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: ''
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: [{
name: 'A + U-Architecture and Urbanism',
data: [<?php
$test_q = mysql_query("SELECT jour_id, journal, citations, year FROM journ_graph WHERE jour_id = '1'");
while($row_q = mysql_fetch_array($test_q)){
$year_q = $row_q['year'];
$citations_q = $row_q['citations'];
echo $citations_q.',';
}
?>]
},{
name: 'AACE International. Transactions of the Annual Meeting',
data: [
<?php
$test_q = mysql_query("SELECT jour_id, journal, citations, year FROM journ_graph WHERE jour_id = '2'");
while($row_q = mysql_fetch_array($test_q)){
$year_q = $row_q['year'];
$citations_q = $row_q['citations'];
echo $citations_q.',';
}
?>]
},{
name: 'AACL Bioflux',
data: [
<?php
$test_q = mysql_query("SELECT jour_id, journal, citations, year FROM journ_graph WHERE jour_id = '3'");
while($row_q = mysql_fetch_array($test_q)){
$year_q = $row_q['year'];
$citations_q = $row_q['citations'];
echo $citations_q.',';
}
?>]
}
?>]
]
});
});
這裏IM手動輸入ID。在where子句(參考代碼)中,圖表正在顯示。 Look below: SELECT jour_id,journal,citations,year FROM journ_graph WHERE jour_id ='3' 在上面的語句中,我手動輸入了id,但我需要它來自第一個表,jour_id。
我也試過,但它不工作。
series: [
<?php
$query_jour = mysql_query("SELECT jour_id, journal, citations, year FROM journ_graph");
$query_journ_count = mysql_num_rows($query_jour);
while($row_journ = mysql_fetch_array($query_jour)){
$jour_id = $row_journ['jour_id'];
?>
{
data: [<?php
$test_q = mysql_query("SELECT jour_id, journal, citations, year FROM journ_graph WHERE jour_id = '$jour_id'");
while($row_q = mysql_fetch_array($test_q)){
$year_q = $row_q['year'];
$citations_q = $row_q['citations'];
echo $citations_q.',';
}
?>]
}
<?php
}?>
我已經使用了兩個while循環但事實上它不工作。請告訴我我哪裏錯了。請幫忙。
如果您需要獲取相關數據,您需要使用[JOIN](http://dev.mysql.com/doc/refman/5.0/en/join.html)。 'SELECT * FROM journ_graph JOIN first_table ON jour_graph.jour_id = first_table.jour_id' – Luke
我在哪裏需要使用連接。請幫忙。我是新的高級圖表。 – razor
您需要停止使用_deprecated_ mysql_ *擴展名,並且請:不要手動將所有數據串起來,我們有'json_encode'來做到這一點 –