Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
 
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. –  Jared Farrish Oct 10 '11 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]] –  user987013 Oct 10 '11 at 3:25
 
Have you checked the web console for javascript issues, or is the script seemingly not executing? –  JackWink Oct 10 '11 at 3:31
 
Instead why would you want to make an ajax call and get the required data from DB using php ? –  Dhiraj Bodicherla Oct 10 '11 at 4:03
add comment

1 Answer

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();
share|improve this answer
 
awesome idea using xml! –  user987013 Oct 10 '11 at 6:50
 
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(); –  user987013 Oct 10 '11 at 6:51
 
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! –  user987013 Oct 10 '11 at 6:58
 
haha problem solved $(data2).find('node').each(function(){ var stuno = $(this).find('snv').text(); var age = $(this).find('agev').text(); –  user987013 Oct 10 '11 at 9:43
 
that's awesome ! please mark the question as answered or else there might be someone trying for the solution and you may also up the answer if you really like it. Thank you very much –  Dhiraj Bodicherla Oct 11 '11 at 10:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.