-1

I'm working on gChart Organization. In PHP i able to get and echo some text from db.

Here i have problem on how to pass text from db to js. In here, i have some text from db ($data[nama] and $data[parnama] ), i want to use they in JS=>['$data[nama]', '$data[parnama]', null],

How i can do that?

Big thanks for the help.

UPDATE

<?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>&nbsp;<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 */;
  • `['']` (or if you have shorthand enabled `['= $data[nama] ?>']`). – Alxandr Jul 17 '13 at 13:00
  • `Notice: Use of undefined constant nama - assumed 'nama'` Please always wrap array keys in single quotes to avoid E_NOTICEs. – feeela Jul 17 '13 at 13:01
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Jul 18 '13 at 17:17

1 Answers1

0

You need to echo them to a javascript variable. I'm not sure why you want the null value though.

Here's the code.

<script type="text/javascript">
  var data = ['<?php echo $data['nama'] ?>',
              '<?php echo $data['paranama'] ?>',
              null];
</script>
Dan Grahn
  • 9,044
  • 4
  • 37
  • 74