我正在研究 gChart 组织。在 PHP 中,我能够从 db 获取并回显一些文本。
在这里,我有关于如何将文本从 db 传递到 js 的问题。在这里,我有一些来自 db ($data[nama]
和$data[parnama]
) 的文本,我想在 JS=> 中使用它们['$data[nama]', '$data[parnama]', null],
我怎么能这样做?
非常感谢您的帮助。
更新
<?php
$host="localhost";
$user="root";
$pass="";
$dbnama="skripsi";
mysql_connect($host, $user, $pass) or die ("Database tidak dapat di akses");
mysql_select_db($dbnama);
$query="select * from tblarya";
$hasil=mysql_query($query);
if($hasil === FALSE) {
die(mysql_error());
}
while ($data=mysql_fetch_array($hasil)){
echo "<label>$data[nama]</label> <label>$data[parnama]</label><br>";}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Testing</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages: ["orgchart"]});
function drawVisualization() {
var data = google.visualization.arrayToDataTable([
//assign all row from db to JS
//for (record 1 upto allrow text in db)
['$data[nama]', '$data[parnama]', null], ]);
// Create and draw the visualization.
new google.visualization.OrgChart(document.getElementById('visualization')).
draw(data, {allowHtml: true});
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 300px; height: 300px;"></div>
</body>
</html>
<html>
<body>
SQL
-- phpMyAdmin SQL Dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Waktu pembuatan: 17. Juli 2013 jam 22:08
-- Versi Server: 5.1.41
-- Versi PHP: 5.3.1
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `skripsi`
--
-- --------------------------------------------------------
--
-- Struktur dari tabel `tblarya`
--
CREATE TABLE IF NOT EXISTS `tblarya` (
`nama` text NOT NULL,
`parnama` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data untuk tabel `tblarya`
--
INSERT INTO `tblarya` (`nama`, `parnama`) VALUES
('A', '-'),
('B', 'A'),
('C', 'A'),
('D', 'B'),
('E', 'B');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;