I m having problem in using the php array values in javascript. I m not very good in javascript so i need a support from u.. $pr[]=array(date('y/m/d',strtotime($rev_date)),$w_s['status_percent']); this is the php array i m using. I need to use its values in the following javascript code

<script id="source" language="javascript" type="text/javascript">
$(function () {
    var datasets = {
        "a": {
            label: "A",
            data: [[1988, 483994], [1989, 479060], [1990, 457648], [1991, 401949], [1992, 424705], [1993, 402375], [1994, 377867], [1995, 357382], [1996, 337946], [1997, 336185], [1998, 328611], [1999, 329421], [2000, 342172], [2001, 344932], [2002, 387303], [2003, 440813], [2004, 480451], [2005, 504638], [2006, 528692]]
        },        
        "B": {
            label: "B",
            data: [[1988, 218000], [1989, 203000], [1990, 171000], [1992, 42500], [1993, 37600], [1994, 36600], [1995, 21700], [1996, 19200], [1997, 21300], [1998, 13600], [1999, 14000], [2000, 19100], [2001, 21300], [2002, 23600], [2003, 25100], [2004, 26100], [2005, 31100], [2006, 34700]]
        }

I need values of $pr in Label: "A" of javascript. the way i used for it is given below but its not working :(

<?php
function data_sets() {
	 $j=count($pr);
	 foreach ($pr as $row) {
	 	
		echo "[";
			$i=0;
			
			foreach ($row as $v){
			$i++;
			//echo " $v ";
			echo " 1000 ";
			if($i==1){
			echo ",";
			}
			}
			$j--;
			if($j!=0){
			echo "],";
			}
			else{
			echo "]";
			}
			} 
			} 

<script id="source" language="javascript" type="text/javascript">
$(function () {
    var datasets = {
        "a": {
            label: "A",
            data: [<?php echo "data_sets()";  ?>]
        },        
        "B": {...... etc

?>

Plz help me :(

Member Avatar for diafol

read up on json. If using relatively recent php version, this could be useful.


<?php
$arr = array ('first'=>1,'second'=>2,'third'=>3,'fourth'=>4);
$myarr = json_encode($arr);
?>


<script id="source" language="javascript" type="text/javascript">
...
var myJSArray = <?php echo $myarr;?>;
...
</script>

Is this what you need?

Thnx! yes its working! ha ha ha ha
:icon_cheesygrin:
Thnx again!

One more problem!
I hv used ur code in followin way.

<?php
$myarr=json_encode($pr);
?>
<script id="source" language="javascript" type="text/javascript">
$(function () {
    var datasets = {
        "a": {
            label: "A",
            data: <?php echo $myarr;  ?>
        },        
        "b": {
            label: "B",
            data: <?php echo $myarr;  ?>
        },
        "c": {
            label: "C",
            data: <?php echo $myarr;  ?>
        },
        "d": {
            label: "D",
            data: <?php echo $myarr;  ?>
        },
        "e": {
            label: "E",
            data: <?php echo $myarr;  ?>
        },
        "f": {
            label: "F",
            data: <?php echo $myarr;  ?>
        },
        "g": {
            label: "G",
            data: <?php echo $myarr;  ?>
        }
    };

Actually i m using the above values to create a line graph bt the problem is that on x-axis the values are date in y/m/d formate of php date() function. When json_encode() function i.e. $myarr=json_encode($pr); , is used the formate looks like this

[["09\/04\/23","60%"],["09\/04\/28","60%"]] i have echo'ed it.

and date values are not being ploted on the graph also...
what to do now?
:confused:

Replace

<?php
  $myarr=json_encode($pr);
?>

with

<?php
  $myarr=(str_replace("\\/", "/", json_encode($pr);
?>

provided there are no other occurrences of "\/" in the converted array.

Recently, I sort-of had to deal with this myself, as I was writing an ECMAScript program that required I convert arrays and objects to plain strings and back. (For reference, see my LTC configurator.) I looked at the JSON stuff, but decided it wouldn't work for me because the conversions needed to be both human- and ES-readable. So I wrote my own function that generates a text-version of the object that can be evaluated.

Member Avatar for diafol

Nice one Fest - like it.

Ahhhhhhhhhh!
Formate is ok now but still no ploting of x-axis points
check out the code guys!
wats the with it?

<script id="source" language="javascript" type="text/javascript">
//var myJSArray= <?php //echo $myarr;  ?>;
$(function () {
    var datasets = {
        "a": {
            label: "A",
            data: <?php echo $myarr;  ?>
        },        
        "b": {
            label: "B",
            data: <?php echo $myarr;  ?>
        },
        "c": {
            label: "C",
            data: <?php echo $myarr;  ?>
        },
        "d": {
            label: "D",
            data: <?php echo $myarr;  ?>
        },
        "e": {
            label: "E",
            data: <?php echo $myarr;  ?>
        },
        "f": {
            label: "F",
            data: <?php echo $myarr;  ?>
        },
        "g": {
            label: "G",
            data: <?php echo $myarr;  ?>
        }
    };

    // hard-code color indices to prevent them from shifting as
    // countries are sturned on/off
    var i = 0;
    $.each(datasets, function(key, val) {
        val.color = i;
        ++i;
    });
    
    // insert checkboxes 
    var choiceContainer = $("#choices");
    $.each(datasets, function(key, val) {
        choiceContainer.append('<br/><input type="checkbox" name="' + key +
                               '" checked="checked" >' + val.label + '</input>');
    });
    choiceContainer.find("input").click(plotAccordingToChoices);

    
    function plotAccordingToChoices() {
        var data = [];

        choiceContainer.find("input:checked").each(function () {
            var key = $(this).attr("name");
            if (key && datasets[key])
                data.push(datasets[key]);
        });

        if (data.length > 0)
            $.plot($("#placeholder"), data, {
                yaxis: { min: 0 },
                xaxis: { tickDecimals: 0 }
            });
    }

    plotAccordingToChoices();
});
</script>

Hellllllllllllllllllllllllp me!
plzzzzzzzzzzzzzzzzz!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.