Jump to content

Parse PHP array


dfowler

Recommended Posts

Hey guys, I tried opening this on the Javascript board.  Somebody recommended AJAX, so here I am.  I know basically nothing of AJAX, so any help would be GREATLY appreciated.

 

I created a form that a user fills out to help determine a plan for them.  The form has three questions (how many meetings, for how long, how often).  After a user inputs the information they click a 'Calculate' button.  Using Javascript the user's inputs are multiplied to get a total estimate of minutes.  I need to take this estimate and parse a PHP array to find the corresponding plan that the estimate is closest too without going over.  For example, if the number comes out to be 450, I want to get the information about the 500 minute plan.  If it is 501, I want the 1000 minute plan.

 

My problem is that I can't figure out how to get the information I need from the php array.  I know PHP is serverside and Javascript is clientside.  So how would I get the results I need?  Is there a way to convert the php array into something I can use with the javascript?  I'll try to give as much code as I can to help out.  First here is how the array is built:

$xml = simplexml_load_file("xml/plans.xml");
foreach($xml->$_SESSION['product']->plans->$term->children() as $q) {
   foreach($q->children() as $p=>$k) {
      if($p == 'minutes') {
         $tag = trim($k);
      }
      $signupPlans2[$tag][$p] = trim($k);
      if($p == 'type'){
         $tag = "";
      }
   }
}

It ends up looking something like this:

Array
(
    [500] => Array
        (
            [minutes] => 500
            [price] => 39.95
            [description] => 1 Year; 500 minutes
            [type] => audioOnly
        )

    [1000] => Array
        (
            [minutes] => 1000
            [price] => 78.95
            [description] => 1 Year; 1,000 minutes
            [type] => audioOnly
        )

    [1500] => Array
        (
            [minutes] => 1500
            [price] => 117.95
            [description] => 1 Year; 1,500 minutes
            [type] => audioOnly
        )

    [2000] => Array
        (
            [minutes] => 2000
            [price] => 153.00
            [description] => 1 Year; 2,000 minutes
            [type] => audioOnly
        )

)

Here is the javascript attached to the 'Calculate' button:

function calculate() {
   var t2 = document.getElementById('textfield2').value;
   var t3 = document.getElementById('textfield3').value;
   var t4 = document.getElementById('textfield4').value;
   var total = t2 * t3 * t4;
   document.getElementById('total').innerHTML = total;
   document.getElementById('step2').style.display = "block";
}

The step2 element is where I want to show the correct plan.  Thanks for any help guys!

Link to comment
Share on other sites

you need to output it as JSON there are php classes and functions you can get that convert arrays into JSON and then you can simply assign them toi a variable in javascript and access them as person[0].arm

 

http://uk3.php.net/manual/en/function.json-encode.php

 

 

do an AJAX call to get the JSON from teh PHP file

 

 

if your PHP array is a result from a sql query then you need to convert it to an array first i think you can use toArray or somthing .

 

 

or you can output all teh plans as a javascript variable array before the scripot is written using php and then just check against that array .

Link to comment
Share on other sites

i think you could do this more easily with just javascript.

 

this is a quick example:

function calculate() {
   var t2 = document.getElementById('textfield2').value;
   var t3 = document.getElementById('textfield3').value;
   var t4 = document.getElementById('textfield4').value;
   var total = t2 * t3 * t4;
   document.getElementById('total').innerHTML = total;
   document.getElementById('step2').style.display = "block";

var minutes;
var price;
var description;
var type;

if(total < 500){
minutes = 500;
price = 39.95;
description = "1 year, 1000 minutes";
type = "audioOnly";
}
alert(description);
}

Link to comment
Share on other sites

i think you could do this more easily with just javascript.

 

this is a quick example:

function calculate() {
   var t2 = document.getElementById('textfield2').value;
   var t3 = document.getElementById('textfield3').value;
   var t4 = document.getElementById('textfield4').value;
   var total = t2 * t3 * t4;
   document.getElementById('total').innerHTML = total;
   document.getElementById('step2').style.display = "block";

var minutes;
var price;
var description;
var type;

if(total < 500){
minutes = 500;
price = 39.95;
description = "1 year, 1000 minutes";
type = "audioOnly";
}
alert(description);
}

The problem is that the array is different and dynamically generated based on some other variables.  So I won't know the exact plans that will be in the array.  Logically it seems simple enough:

If the total is less than the minutes in array, move on, otherwise use that plan.

 

Link to comment
Share on other sites

you need to output it as JSON there are php classes and functions you can get that convert arrays into JSON and then you can simply assign them toi a variable in javascript and access them as person[0].arm

 

http://uk3.php.net/manual/en/function.json-encode.php

 

 

do an AJAX call to get the JSON from teh PHP file

 

 

if your PHP array is a result from a sql query then you need to convert it to an array first i think you can use toArray or somthing.

 

 

or you can output all teh plans as a javascript variable array before the scripot is written using php and then just check against that array .

The php array is from an external XML file I created that lists every single possible plan (there are a lot of them).  For the JSON, I know I can't install or modify the server settings.  Will I be able to include this to get it to work?

Link to comment
Share on other sites

you have an array so just do this

 

echo json_encode($yourArray);

exit; // or woteva

 

 

then in your js do the ajax to get it if you have a framework it will be easy

 

JQUERY

 


$.ajax({
   type: "POST",
   url: "getmyarray.php",
   data: "name=John&location=Boston",
   success: function(msg){
     myArray = eval(msg);

     alert(myArray[0].arm);
     //or
     alert(myArray.arm);
     // or
     for(i=0;i<myArray.length;i++){
            alert(myArray[i].arm);
      }
   }
});

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.