Jump to content

JS within PHP


Mufleeh

Recommended Posts

Hi All,

 

I need to plot a graph retrieving data from my data base. I have done all the codings to get the data and store them to 2 different arrays.I though of using jquery to create the graphs. Below is the script has to be added to generated the grahp. I was wondering how to get this within the function in my php script. Can anyone help me fixing this?

 

 

<script class="code" type="text/javascript">$(document).ready(function(){
        var s1 = [2, 6, 7, 10];
        var ticks = ['a', 'b', 'c', 'd'];
        
        plot1 = $.jqplot('chart1', [s1], {
            seriesDefaults:{
                renderer:$.jqplot.BarRenderer,
                pointLabels: { show: true }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
                }
            },
            highlighter: { show: false }
        });
    
        $('#chart1').bind('jqplotDataClick', 
            function (ev, seriesIndex, pointIndex, data) {
                $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
            }
        );
    });</script>
    
   
        
    <div id="chart1" style="margin-top:20px; margin-left:20px; width:300px; height:300px;"></div>


 

 

Regards,

Mufleeh

Link to comment
https://forums.phpfreaks.com/topic/242115-js-within-php/
Share on other sites

Hi,

 

Thanks for your support. My arrays are one dimensional and stored in 2 variables like $arrayOne and $arrayTwo. As you see in the above java script(example) there are two variables s1 and ticks have been defined and made them equal to 2 arrays that comes in to the graph x and y.

 

If we keep the js out of php we can't make it as below right?

 

var s1= $arrayOne

var ticks = $arrayTwo

 

So how can I do this?

 

 

Regards,

Mufleeh

Link to comment
https://forums.phpfreaks.com/topic/242115-js-within-php/#findComment-1243418
Share on other sites

Hi,

 

Thanks for your response. Do you mean that if I write the above code inside PHP then I can use those variables inside js as an Array? It doesn't work as I expected. I have given you how my code looks like, can you figure out what could we do?

 

 

 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <link rel="stylesheet" type="text/css" href="css/jquery.jqplot.css" />
  <link rel="stylesheet" type="text/css" href="css/examples.css" />
  
  <script language="javascript" type="text/javascript" src="js/jquery.js"></script>
  <script language="javascript" type="text/javascript" src="js/jquery.jqplot.js"></script>
  <script language="javascript" type="text/javascript" src="js/jqplot.barRenderer.js"></script>
  <script language="javascript" type="text/javascript" src="js/jqplot.categoryAxisRenderer.js"></script>
  
</head>
<body>						


					<?php // PHP starts

					function myFunction(){ // function starts here

					//function codes here				 



					"var s1 = jQuery.parseJSON(".json_encode($stackDiv).");";
					"var ticks = jQuery.parseJSON(".json_encode($stackCount).");";

					?> // PHP closed
                        
                        // java script outside PHP

                        <script class="code" type="text/javascript">$(document).ready(function(){


					plot1 = $.jqplot('chart1', [s1], {
						seriesDefaults:{
							renderer:$.jqplot.BarRenderer,
							pointLabels: { show: true }
						},
						axes: {
							xaxis: {
								renderer: $.jqplot.CategoryAxisRenderer,
								ticks: ticks
							}
						},
						highlighter: { show: false }
					});

					$('#chart1').bind('jqplotDataClick', 
						function (ev, seriesIndex, pointIndex, data) {
							$('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
						}
					);
				});</script>
    
   
        
  				  <div id="chart1" style="margin-top:20px; margin-left:20px; width:300px; height:300px;"></div>

                        
                       				 <?php  // PHP reopened


		} // myFunction function close



				other functions and codes


		?> // PHP closed



</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/242115-js-within-php/#findComment-1243437
Share on other sites

Hi,

 

I am sorry its bit confusing to me. When I made it as below inside my php script,

 

echo "var s1 = jQuery.parseJSON(".json_encode($stackDiv).");";
echo "var ticks = jQuery.parseJSON(".json_encode($stackCount).");";

 

Browser displays the data inside the arrays as below.

 

var s1=jQuery.parseJSON([22,32,45,56])

 

var ticks=jQuery.parseJSON([12,35,32,11])

 

 

 

But how can I pass this into javascript out side the php (in the same page) and get the graph displayed?

Link to comment
https://forums.phpfreaks.com/topic/242115-js-within-php/#findComment-1243444
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.