-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 */;
3

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Or var data = <?php echo json_encode( $data ); ?>;
@screenmutt Hi, i have try your codes, but it seems no give result. the result show blanks. I have update my quest, mybe you can help that. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.