0

my final goal is to convert data from database to a array, then i can print a pretty nice graph with a jquery plugin "flot" in html file:

<script language="javascript" type="text/javascript" src="jquery.flot.js"></script>
<script type="text/javascript" >
$(function test() {   
var d3= [[0, 3], [4, 8], [8, 5], [9, 13]];   
$.plot($("#placeholder"), [d3]);
});
</script> 

in php file

echo"<script language='javascript' >\n";  
$k = 0;  
    $agevi = mysql_result($result, $k, "Age"); 
    $snvi = mysql_result($result, $k, "StuNo"); 
settype($agevi, "integer"); 
settype($snvi, "integer"); 
$smallarray = array($snvi,$agevi);  
    $bigarray = array($smallarray);
    $i++;   
while ($i < $number) 
    { 
      $agev = mysql_result($result, $i, "Age"); 
  $snv = mysql_result($result, $i, "StuNo");  
  settype($agev, "integer"); 
  settype($snv, "integer");  
  $smallarray = array($snv,$agev);   
  array_replace($smallarray,$smallarray);
  array_push($bigarray,$smallarray); 
  echo"\n"; 
  $i++;    
  }  
  echo 'var stunoarr = '.json_encode($bigarray).';'; 
  }    
      echo"\n</script>";  
  echo  json_encode($bigarray) ; 
      echo "\ndocument.write(\"bigarray array is: <b>" . json_encode($bigarray) .  "</b>\")";    
      mysql_free_result($result); 
      mysql_close();  

what i have in php output is work:

[[1,12],[2,5],[3,6],[4,2],[5,7],[6,2],[7,12],[8,3],[9,6],[10,8],[11,4]] document.write("bigarray array is: [[1,12],[2,5],[3,6],[4,2],[5,7],[6,2],[7,12],[8,3],[9,6],[10,8],[11,4]]")

so people suggesting use json, yes i did, and i also put

<script>
var myvar = <?= json_encode($bigarray); ?>; 
</script>

at the end of php file outside ?>

how exactly should get the variable from php to javascript in order to produce the graph then..? tried hundrd of ways now still not working...thanks in advance! :D

4
  • Making something work with functionality like flot means that you understand what flot expects, in addition to understanding how the language you're using to manipulate the output itself works. Commented Oct 10, 2011 at 3:21
  • flot can be easily used as long as i have integer array, and i made it work in the php file :[[1,12],[2,5],[3,6],[4,2],[5,7],[6,2],[7,12],[8,3],[9,6],[10,8],[11,4]] Commented Oct 10, 2011 at 3:25
  • Have you checked the web console for javascript issues, or is the script seemingly not executing? Commented Oct 10, 2011 at 3:31
  • Instead why would you want to make an ajax call and get the required data from DB using php ? Commented Oct 10, 2011 at 4:03

1 Answer 1

1
in javascript file

<script language="javascript" type="text/javascript" src="jquery.flot.js"></script>
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" >
    $(function test() {
    getData();
    });
    function getData(){
      $.ajax({
                    url: "yourfile.php",
                    type: "GET",
                    data: '',
                    cache: false,
                    success: function (result) {
                        showGraph(result);
                    }
            });
    }
function showGraph(data){
 $(data).find('node').each(key,val){
  var age = $(val).find('agev').text();
  var stuno = $(val).find('snv').text();
  // populate your d3 array here
}
$.plot($("#placeholder"), [d3]);
}
    </script>

in yourfile.php

header('Content-Type: text/xml');

$k = 0;  
    $agevi = mysql_result($result, $k, "Age"); 
    $snvi = mysql_result($result, $k, "StuNo"); 
settype($agevi, "integer"); 
settype($snvi, "integer"); 
$smallarray = array($snvi,$agevi);  
    $bigarray = array($smallarray);
$resultXML = new SimpleXMLElement(stripslashes('<data></data>'));
    $i++;   
while ($i < $number) 
    { 
      $agev = mysql_result($result, $i, "Age"); 
  $snv = mysql_result($result, $i, "StuNo");  
  settype($agev, "integer"); 
  settype($snv, "integer");  
  $temp = $resultXML->addChild('node');
  $temp->addChild('snv',$snv);
  $temp->addChild('agev',$agev);
  $i++;    
  }  

  }    
     mysql_free_result($result); 
      mysql_close();  
echo $resultXML->asXML();
Sign up to request clarification or add additional context in comments.

3 Comments

on html page i tried using array=[]; array.push(age,stuno); and d3.push(array); should be working right? then its not generating graph...so i delete the three line $(data).find('node').each(key,val){ var age = $(val).find('agev').text(); var stuno = $(val).find('snv').text();
on html page i tried using array=[]; array.push(age,stuno); and d3.push(array); should be working right? then its not generating graph...so i delete the three line $(data).find('node').each(key,val){ var age = $(val).find('agev').text(); var stuno = $(val).find('snv').text(); } and define a array[[1,2],[2,3]]; then it works...is it the code having error or should add something else to make it work? thanks so much for helping!
haha problem solved $(data2).find('node').each(function(){ var stuno = $(this).find('snv').text(); var age = $(this).find('agev').text();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.