Jump to content

Getting information from PHP array


dfowler

Recommended Posts

Hey guys, I have quite a problem that I can't seem to figure out.  There is a form a user fills out to help determine a plan for them to choose.  I have a PHP array that is dynamically created featuring several of the plans.  Here is the basic layout of the array:

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
        )

)

 

Now, 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 put through an algorithm to get a total estimate of minutes.  I need to take this estimate and parse the 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?

 

Thanks for any help!

Link to comment
Share on other sites

AJAX

Well, I don't know any AJAX; so I think I'm screwed.  Can anybody help?  Here is the code I am using to get the plans array:

$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 = "";
	}
}
}

 

And here is the javascript to calculate the total number of minutes:

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";
}

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.