Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array calculations- php ->javascript or something else? 1

Status
Not open for further replies.

sjk1000

Programmer
Jan 22, 2009
14
0
0
GB
Hi all
I've been stumped over this for days and days now. I have an array in php that I want to perform calculations on via the user clicking radio buttons in a table. The results should be shown instantly on the page. The radio buttons have several values associated with them (in the php array) that will be used in the calculations when the button is clicked.

I've read about hiding the array as a document object so that javascript can get to it, which I've no experience of doing yet, and I've seen ways of passing the whole array to javascript and then back before the page is submitted, which apparently isn't straightforward. I don't have the experience to say which, if any, of these will work the best.

In summary (just in case I've baffled everyone :) ), I'm trying to display results of instant calculations on values in a php array following the click of a radio button. What's the best method for this?

Any help is very much appreciated. This has driven me more nuts than anything I've done so far. I may go mad soon and I'll take u all with me. :)
Thanks, Steve
 
maybe it would help if you provided an html mock-up of what you are attempting.
 
but if your question here is as a result of your post in the javascript format, and what you are really asking is how to get an array into javascript, then the easiest way is something like this

Code:
$json = json_encode($array);
echo <<<HTML
<script type="text/javascript">
var matharray = {$json};
</script>
HTML;

you then have a javascript object that you can manipulate as you need.
 
Hi, Thanks for your replies. Yes, I was directed here from the javascript forum. Just been doing some housekeeping to tidy the array up hence my slow response. A quick print_r is below. Is it really as simple as using json_encode to get this into javascript? (go on, say that it is)

Array (
[0] => Array ( [products_id] => 13769 [seller_id] => 1 [products_price] => 15.45 [shipping_price] => 3.00 [additional_item_price] => 1.40 [stock_quantity] => 9 )

[1] => Array ( [products_id] => 13769 [seller_id] => 2 [products_price] => 16.99 [shipping_price] => 3.00 [additional_item_price] => 0.50 [stock_quantity] => 0 )

[2] => Array ( [products_id] => 13769 [seller_id] => 3 [products_price] => 10.99 [shipping_price] => 3.20 [additional_item_price] => 1.50 [stock_quantity] => 7 )

[3] => Array ( [products_id] => 13674 [seller_id] => 1 [products_price] => 25.40 [shipping_price] => 3.00 [additional_item_price] => 1.40 [stock_quantity] => 0 )

[4] => Array ( [products_id] => 13674 [seller_id] => 2 [products_price] => 10.99 [shipping_price] => 3.20 [additional_item_price] => 0.50 [stock_quantity] => 9 )

[5] => Array ( [products_id] => 13674 [seller_id] => 3 [products_price] => 16.45 [shipping_price] => 3.20 [additional_item_price] => 1.50 [stock_quantity] => 6 ) )





)

ps sorry, not sure how to format the above in the
 
json_encode should encode the array for you just fine.
 
Thanks a lot for your help jpadie. Just out to the cinema. Will give it a try tomorrow.
Cheers, Steve
 
Thanks jpadie, this works perfectly. Exactly what I needed. Fingers crossed that the decode is as good. : )
If anybody needs some background reading the link below is a good starter to what json is and what the encode/decode is doing:
Thanks again, Steve
 
i don't think that you need to decode. when it's in javascript it is already a javascript object. just use as you like. the kind folks in the js forum will be able to tell you how to address a javascript object if you're unclear.

one note: json_encode is quite type specific and so will enquote integer/numeric values if php thinks they are strings.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top