• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 98
  • Last Modified:

php to jQuery

How do I get this data from php to a jquery file so I can use it in a chart? The php has to be on another page because I have something else on there that I eventually need to use for the query. I have everything working separately but now have to integrate them. I'm very new to all coding languages.
data.php
	$query = "SELECT * FROM tracking where userid=1";		
	$result = mysqli_query($connection, $query);

	while($row = mysqli_fetch_array($result)) {
			$mind[] = $row['mind'];
			$body[] = $row['body'];
			$soul[] = $row['soul'];
			}

$mindData = implode(",", $mind);
$bodyData = implode(",", $body);
$soulData = implode(",", $soul);



  <script id="source" language="javascript" type="text/javascript">
$.ajax({                                      
  url: 'data.php', data: "", dataType: 'json',  success: function(rows)        
  {
???
    for (var i in rows)
    {
      var row = rows[i];          

      var mind = ??
      var body = ??
      var soul = ??

  </script>
  <script id="source" language="javascript" type="text/javascript">

so I can use it in a chart...

                name: 'Mind',
                type: 'spline',
                data: [mind],
                tooltip: {
                    valueSuffix: '0'
                },
				
				        }, {
                name: 'Body',
                type: 'spline',
                data: [body],
                tooltip: {
                    valueSuffix: '0'
                },
							
							        }, {
                name: 'Soul',
                type: 'spline',
                data: [soul],
                tooltip: {
                    valueSuffix: '0'
                },
  </script>

Open in new window

0
designaire
Asked:
designaire
  • 10
  • 6
1 Solution
 
Ray PaseurCommented:
This is a fairly complicated application and it might be better for you to start with a simpler learning example.  Here is where I might start, just to get the concepts nailed down.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html
0
 
GaryCommented:
The easiest solution would be to return an array of the json_encoded arrays

$ajax = array();
$ajax[0]=json_encode($mind);
$ajax[1]=json_encode($body);
$ajax[2]=json_encode($soul);

echo json_encode($ajax)

Open in new window

Then in the jQuery

$.ajax({                                      
  url: 'data.php', data: "", dataType: 'json',  success: function(rows)        
  {
    mind_json=rows[0];
    body_json=rows[1];
    soul_json=rows[2];
}
...

Open in new window

0
 
designaireAuthor Commented:
It looks good but even if it works I can't get it into the chart. This is the code they give you for json. How would I integrate it with the below code:  It is a Highchart.


  $.getJSON('data.json', function(data) {
        options.series[0].data = data;
        var chart = new Highcharts.Chart(options);
    });
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
GaryCommented:
What is the chart expecting?
Is this...
options.series[0].data = data;

...for all the data or supposed to be one block of the data like mind in which case you would just do
options.series[0].data = data[0];

Using the method I talk about earlier you have a json string containing three elements, each of those elements contain the json data for your mind, body etc
0
 
designaireAuthor Commented:
I can use this from javascript:

var plainData=[6,5,4,16]    

name: 'Mind',
                type: 'spline',
                data:plainData,
                tooltip: {
                    valueSuffix: '0'


or if I used data

name: 'Mind',
                type: 'spline',
                data:[2,3,5,2],
                tooltip: {
                    valueSuffix: '0'
                },

This works for php
                name: 'Mind',
                type: 'spline',
                data: [<?php echo join($soul, ',') ?>],
                tooltip: {
                    valueSuffix: '0'
                },




It's Highcharts and have this is what have for json. I know the chart I'm using starts out with chart:{

$(document).ready(function() {

    var options = {
        chart: {
            renderTo: 'container',
            type: 'spline'
        },
        series: [{}]
    };

    $.getJSON('data.json', function(data) {
        options.series[0].data = data;
        var chart = new Highcharts.Chart(options);
    });

});
0
 
designaireAuthor Commented:
I'm trying to use it with php data that's on another page, otherwise I would just keep the php. I can understand if it's too involved.
0
 
designaireAuthor Commented:
It won't take any data inside the ajax unless there's something I'm doing wrong.
Even if I put the javascript inside the ajax it won't work.
0
 
GaryCommented:
Back up a bit

$.getJSON('data.json', function(data) {

What is data.json? I thought you were making a call to a php page for the json data?
0
 
designaireAuthor Commented:
I want to bring the php data from another php page to the javascript  page so I can use it in the JQuery chart.


var plainData=[6,5,4,16]    

 name: 'Mind',
                 type: 'spline',
                 data:plainData,
                 tooltip: {
                     valueSuffix: '0'
0
 
GaryCommented:
data.json needs to be the name of the php page that is returning the json data
0
 
designaireAuthor Commented:
I haven't had time to work on this but it's pretty complex.
0
 
designaireAuthor Commented:
I'll have to shut this down for now
0
 
designaireAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for designaire's comment #a40311211

for the following reason:

I got somebody to help me
0
 
GaryCommented:
Delete the question, there is no answer here
0
 
designaireAuthor Commented:
This seemed to be working but recently on my localhost it's giving me last month first. It seems to work on line however.

date_default_timezone_set('America/New_York');
$month = (int) (isset($_GET['month']) ? $_GET['month'] : date('m'));

if ($month=="") 
{  

	$dateTime = date(DATE_RSS);       
	$dateTime . "<br>" . "<br>";

	"date('m') =" . date('m') . "<br>" . "<br>"; 
	 
	 $month = ltrim(date('m'),0) ;  
	
	 "ltrim(date('m'),0) =" . $month . "<br>" . "<br>";  
}

$prevmonthtopost = ((($month+11)-1) % 12) + 1; 
$nextmonthtopost = ((($month+11)+1) % 12) + 1;  

	$dateObj   = DateTime::createFromFormat('!m', $month);
	$monthName = $dateObj->format('F'); // March
	 
	 $query = "SELECT * FROM tracking where userid=$id and month=$month ORDER BY day";	
	 $result = mysqli_query($connection, $query);
	 if (!$result){
		 die("doesn't work: " .
		     mysqli_connect_error() .
		     "(" . mysqli_connect_errno() . ")"
		 );		
	  }
	while($row = mysqli_fetch_array($result)) {
		$day[] = $row['day'];
		$mind[] = $row['mind'];
		$body[] = $row['body'];
		$soul[] = $row['soul'];

	}

Open in new window

0
 
GaryCommented:
Seriously! A 4 month old question that you abandoned and now you are coming back with something completely different ???
0
 
designaireAuthor Commented:
I found the answer somewhere else. It's just a prototype and works for what I need it for.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now