[英]Word cloud using jQcloud won't appear
我正在嘗試為json創建詞雲。 我一直在參考有關如何使用jQcloud進行字雲的頁面http://mistic100.github.io/jQCloud/demo.html
最初沒有任何錯誤,但是現在控制台上出現錯誤,表明它無法在字符串'{text:“ mark”,weight:8}'上創建屬性'weight'。 我還是新手。 如果有人可以指出錯誤,那就太好了。 編碼在我的process.php文件中。
if(isset($_POST['submit'])){
$data = $_POST["d2"];
$obj = json_decode($data, TRUE);
$item = array();
foreach($obj as $key => $value)
{
$item[] = '{text: "'.$key.'", weight: '.$value.'}';
$sql = "SELECT word FROM test WHERE word = '$key'";
$r = mysql_query($sql) or die("Error: " . mysql_error());
$row0 = mysql_num_rows($r);
if($row0 != 0)
{
$found = "UPDATE test SET weight = $value WHERE word = '$key'";
mysql_query($found) or die ("Error: " . mysql_error());
}
}
echo print_r($item);
}?>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="jqcloud.css" />
<script type="text/javascript" src="jqcloud-1.0.4.js"></script>
<script type="text/javascript">
var varNameSpace = <?php echo json_encode($item); ?>;
varNameSpace = JSON.parse(JSON.stringify(varNameSpace));
alert(varNameSpace);
$(function() {
$("#d").jQCloud(varNameSpace);
});
</head>
<body>
<div id="d" style="width: 550px; height: 350px; border: 1px solid #ccc;"></div>
</body>
</html>
當我提醒varNameSpace時 ,示例輸出將如下所示:
{text: "mark", weight: 8},
{text: "zuckerberg", weight: 4},
{text: "money", weight: 2},
{text: "man", weight: 2},
{text: "and", weight: 7},
{text: "having", weight: 1},
{text: "apart", weight: 8},
{text: "rich", weight: 2},
{text: "of", weight: 3},
{text: "world", weight: 2},
{text: "less", weight: 1}
這是控制台上echo json_encode($ item)的示例輸出:
["{text: \"mark\", size: 8}",
"{text: \"apart\", size: 8}",
"{text: \"and\", size: 7}",
"{text: \"zuckerberg\", size: 4}" ...and many more];
當我使用print_r回顯$ item時 ,這是示例輸出:
Array ( [0] => {text: "mark", size: 8} [1]
=> {text: "apart", weight: 8} [2]
=> {text: "and", weight: 7} [3]
=> {text: "zuckerberg", size: 4} ...and many more ) 1
您正在將純JSON格式的字符串輸出到變量。 使用JSON.parse()
將其包裝以將其轉換為對象:
var varNameSpace = <?php echo json_encode($item); ?>;
varNameSpace = JSON.parse(varNameSpace);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.