mysql - use array in javascript foreach -
i have next (simplified) php function calculates total value me. fetch array of items wordpress database , checks type of each item , compare value specific given value , sums comparisons up.
function total(){ $value1 = 10; // compare value type 1 items $value1 = 20; // type 2 items $value1 = 30; // type 3 items $total = 0; // reset total variable global $wpdb; $items = $wpdb->get_results("select `item_type` type, `item_value` value `table`"); // items database foreach ($items $item) { // check item type switch ($item->type) { case 1: $total = $total + min($value1, $item->value); break; case 2: $total = $total + min($value2, $item->value); break; case 3: $total = $total + min($value3, $item->value); break; }} homecoming $total; } now want remake same function in javascript can utilize in form calculations. got far;
<script type="text/javascript"> function calculate_total (){ var items = <?php echo json_encode($items); ?>; var value1 = document.form.range1input.value; var value2 = document.form.range2input.value; var value3 = document.form.range3input.value; each (var item in items){ switch (**(refer item type in array)**) { case 1: total = total + min(value1, **(refer item value in array)**); break; case 2: total = total + min(value2, **(refer item value in array)**); break; case 3: total = total + min(value3, **(refer item value in array)**); break; }} document.form.total.value = total; } </script> how can refer values in items array? hope can give me advice on one.
also saw echo json_encode($items) formats array {"type":"3","value":"1.00"},{"type":"1","value":"20.50"} etc etc, wondering if usable format javascript because when seek total = items[1]; see [object object] in form.
$items = $pdb->get_results() btw formatted array ( [0] => stdclass object ( [type] => 1 [value] => 35.00 ) [1] => stdclass object ( [type] => 3 [value] => 1.00 )
like speculated, first item can referred items[0], sec items[1], , on.
so, in switch statement / within loop item refers current index (0, 1, 2, etc), can items[item] refer item you're looking @ in loop: first equal items[0], items[1], , on.
so, switch (items[item].type) { should need.
(a quick js recap: arrays , objects different things in js, differences if you're not sure. when referencing them, can utilize myarray[0] things array, , can utilize myobject['mykey'] or myobject.mykey things object. in case, items array, , each of elements object. can hence items[0]['type'] or items[0].type access type of first item).
the reason items[1] showed [object object] when tried because browsers aren't great @ displaying objects on screen – object there, , js can see it, it's objects printed [object object]. you'd improve off displaying json.stringify(items[1]) see json representation of object string. improve yet, seek console.log(items) or console.log(items[1]) see entire object in browser's console (you can search how bring browser console, it's different each browser/os, it's brilliant debugging).
javascript mysql arrays foreach json
No comments:
Post a Comment