我知道很多函数可以删除php中的char。但我很困惑,删除这个: 我想从
删除“,”<?php
include '../config/settings.php';
$query = "SELECT keyword,count(*) AS jumlah
FROM search GROUP BY keyword ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$data= "['$row[keyword]', $row[jumlah]],<br>";
echo $data;}
?>
将给出结果:
['Smartfren', 1],
['Telkomsel', 1],
我希望输出如下:
['Smartfren', 1],
['Telkomsel', 1]
怎么做?
非常感谢你的回应。更新
根据@orangpill的回答说我打印的数据如下:
['ABC', 6597],
['XYZ', 4479],
['PQR', 2075],
['Others', 450]
我想制作一张图表,说js图表的代码必须像:
data: [
['Firefox', 45.0],
['IE', 26.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
]
基于@orangepill现在我的代码:
<?php
include '../config/settings.php';
$query = "SELECT keyword,count(*) AS jumlah
FROM search GROUP BY keyword ASC";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_array($result))
{
$data[] = "['$row[keyword]', $row[jumlah]]";
}
echo implode(",<br/>", $data);
?>
<script type="text/javascript">
$(function () {
$('#container').highcharts({
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Presentase Sentimen Positif'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage}%</b>',
percentageDecimals: 1
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' %';
}
}
}
},
series: [{
type: 'pie',
name: 'Nilai',
data: [
<?php while($row = mysql_fetch_array($result))
{
$data[] = "['$row[keyword]', $row[jumlah]]";
}
echo implode(",<br/>", $data); ?>
]
}]
});
});
</script>
答案 0 :(得分:2)
通过构建数组并在想要输出时将其插入
,这可能是有意义的。<?php
include '../config/settings.php';
$query = "SELECT keyword,count(*) AS jumlah
FROM search GROUP BY keyword ASC";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_array($result))
{
$data[] = "['$row[keyword]', $row[jumlah]]";
}
echo implode(",<br/>", $data);
?>
如果您要格式化此数据供javascript使用,更好的方法是使用json_encode。
<?php
include '../config/settings.php';
$query = "SELECT keyword,count(*) AS jumlah
FROM search GROUP BY keyword ASC";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_array($result))
{
$data[] = array($row[keyword], (int)$row[jumlah]);
}
echo "var data = ".json_encode($data).";";
?>
答案 1 :(得分:1)
您还可以使用substr()
功能:
$data = "";
while($row = mysql_fetch_array($result)) {
$data .= "['$row[keyword]', $row[jumlah]],<br>";
}
$data = substr($data, 0, -5);
echo $data;
答案 2 :(得分:0)
rtrim():
$data = rtrim($data, ',');
或者,在您的特定示例中,substr:
$data = substr($data, 0, -5);
也删除“
”。