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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic tables and php arrays - Help please!

Status
Not open for further replies.

sjk1000

Programmer
Jan 22, 2009
14
GB
Hi all
I'd be really grateful if someone could tell me the correct method for dealing with the following problem. See attached mock-up of my site's order table.

The user is allowed to change the quantity of items (input fields in the green box) to select which suppliers to order from, and based on this the values in the red boxes should be calculated.

It should be a simple problem of multiplying quantities by prices and summing across suppliers for each product... but I want to do it in javascript without reloading the page : )

All values are taken from my database and stored in a php associative array - which seems to be the main complicating factor as it's quite a deep one. eg

$products[13789]['sellers'][3]['products_price'] = 16.99

How would you guys tackle this? Are there any examples/tutorials out there you can point me to?
Thanks, Steve

ps not sure if I can attach a file in the usual way. Apologies if the image doesn't appear
 
Hi

Something like this :
PHP:
echo "<script type=\"text/javascript\">
var products=",json_encode($products),"
</script>";
JavaScript:
alert(products[13789]['sellers'][3]['products_price'])
Or you can build the products object in JavaScript "manually" like this :
Code:
var products={
  13789:{
    'sellers':{
      3:{
        'products_price':16.99
      }
    }
  }
}
Or you can use AJAX to do the calculations with PHP on server side, without reloading the page. See forum1600 for more.

Feherke.
 
Without seeing any code, all I can offer is this simple step-by-step guide:

1. Loop over all table rows
2. From the current row, get the qty
3. From the current row, get the price
4. Multiply the two together
5. Store the result in the total field in the current row
6. Repeat from step 2 until all rows are covered

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi
The image upload obviously failed badly..d'oh..so I've uploaded an annotated screenshot to flickr here


The php array that I'm working with is below. I've underlined an example of the product code and highlighted in bold how each supplier's pricing details for that product are stored in the array. Looking at the screenshot, can you suggest a method as to how I can make dynamic updates to the fields in the red boxes based on the the quantity values changed by the user in the green box and the values stored in this php array. Can I get this whole array to javascript?
Thanks, Steve

Array ( [13769] => Array ( [qty] => 1 [products_name] => Product 1 [sellers] => Array ( [3] => Array ( [products_price] => 10.99 [shipping_price] => 3.2 [additional_item_price] => 1.5 [stock_quantity] => 7 [seller_company] => Supplier 3 ) [1] => Array ( [products_price] => 15.45 [shipping_price] => 3 [additional_item_price] => 1.4 [stock_quantity] => 9 [seller_company] => Supplier 1 ) ) ) [13674] => Array ( [qty] => 10 [products_name] => Product 2 [sellers] => Array ( [2] => Array ( [products_price] => 10.99 [shipping_price] => 3.2 [additional_item_price] => 0.5 [stock_quantity] => 9 [seller_company] => Supplier 2 ) [3] => Array ( [products_price] => 16.45 [shipping_price] => 3.2 [additional_item_price] => 1.5 [stock_quantity] => 6 [seller_company] => Supplier 3 ) ) ) [13931] => Array ( [qty] => 1 [products_name] => Product 3 [sellers] => Array ( [2] => Array ( [products_price] => 10.99 [shipping_price] => 3.5 [additional_item_price] => 0.5 [stock_quantity] => 17 [seller_company] => Supplier 2 ) [3] => Array ( [products_price] => 12.34 [shipping_price] => 3.2 [additional_item_price] => 1.5 [stock_quantity] => 4 [seller_company] => Supplier 3 ) [1] => Array ( [products_price] => 12.5 [shipping_price] => 3 [additional_item_price] => 1.4 [stock_quantity] => 4 [seller_company] => Supplier 1 ) ) ) )
 
Many thanks for the replies.

Feherke, your json solution works perfectly. I'll next try integrating the js array into functions to carry out the calculations when the page loads and onclick.

When the form is submitted, I'd planned that php should pass only the quantity values as session variables to the next page and carry out identical calculations as done in javascript. Does this make sense as a method of getting changes server side?
Thanks, Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top