I’m trying to make an inventory system as part for my e-commerce project so I need to get the price per product. The problem is when I use alert to display the data it combined. My main goal is to get the price per product (row) so I can compute it for the inventory
The problem. the 2 product price combined. how can i separate them?:
JavaScript
x
174
174
1
function addtocart()
2
{
3
var sendtotal = $(".total").html();
4
var address = $('#email-field').val();
5
var pts = $('#pts').val();
6
var totals = $('.total').text();
7
var rewards =(parseFloat(totals)*parseFloat(pts)).toFixed(2);
8
$('.qty').prop('disabled',true);
9
var schedule = $('#deliveryschedule').val();
10
var brgy = $('#brgy').val();
11
var price = $('.prodtotal').text();
12
13
alert(price)
14
15
var form_data={
16
rewards: rewards,
17
sendtotal: sendtotal,
18
address: address,
19
/* schedule: $('#schedule').val(),
20
brgy: $('#brgy').val(),*/
21
};
22
23
$.ajax({
24
async : 'true',
25
url:"<?php echo base_url() ?>admin/itexmo/",
26
data: form_data,
27
method:"post",
28
cache: false,
29
success: function (savingStatus)
30
{
31
Swal.fire(
32
'Sucess!',
33
'Wait for a rider!',
34
'success'
35
)
36
37
},
38
error: function (xhr, ajaxOptions, thrownError) {
39
alert("Error Encountered While Saving The Events.");
40
}
41
});
42
43
var timer2 = "1:00";
44
var interval = setInterval(function() {
45
46
47
var timer = timer2.split(':');
48
//by parsing integer, I avoid all extra string processing
49
var minutes = parseInt(timer[0], 10);
50
var seconds = parseInt(timer[1], 10);
51
--seconds;
52
minutes = (seconds < 0) ? --minutes : minutes;
53
seconds = (seconds < 0) ? 59 : seconds;
54
seconds = (seconds < 10) ? '0' + seconds : seconds;
55
//minutes = (minutes < 10) ? minutes : minutes;
56
$('.countdown').html(minutes + ':' + seconds);
57
if (minutes < 0) clearInterval(interval);
58
//check if both minutes and seconds are 0
59
if ((seconds <= 0) && (minutes <= 0)) clearInterval(interval);
60
timer2 = minutes + ':' + seconds;
61
62
if ((seconds == 0) && (minutes == 0))
63
{
64
$('#test').prop('disabled',true);
65
localStorage.setItem('disabled', '#test');
66
67
}
68
69
}, 1000);
70
71
$('#btncheckout').hide();
72
$('#btnfinish').attr("hidden",false);
73
74
75
}
76
77
78
<table class="table table-hover" id="shoppingcart" style="position: relative;
79
top: 125px;">
80
<thead>
81
82
<tr>
83
<th>Product</th>
84
<th>Restaurant</th>
85
<th>Quantity</th>
86
<th class="text-center">Price</th>
87
<th class="text-center">Total</th>
88
<th> </th>
89
</tr>
90
</thead>
91
92
<?php foreach ($cart as $value): ?>
93
<tbody>
94
<tr>
95
<td class="col-sm-8 col-md-6">
96
<div class="media">
97
<a class="thumbnail pull-left" href="#"> <img class="media-object"
98
src="http://icons.iconarchive.com/icons/custom-icon-design/flatastic-2/72/product-icon.png"
99
style="width:
100
72px; height: 72px;"> </a>
101
<div class="media-body">
102
<h4 class="media-heading"><a href="#"><?php echo $value['product_name'] ?></a></h4>
103
<h5 class="media-heading"> by <a href="#"><?php echo $value['restaurant_name'] ?></a></h5>
104
<span>Status: </span><span class="text-warning"><strong><?php echo $value['status'] ?></strong>
105
</span>
106
</div>
107
</div></td>
108
<td class="col-md-1 text-left"><strong class="label label-danger">None</strong></td>
109
<td class="col-sm-1 col-md-1 quan" style="text-align: center">
110
<input type="email" class="form-control qty" id="qty" name="qty" value="">
111
</td>
112
<div class="calculate">
113
<td class="col-sm-1 col-md-1 text-center"><strong class="prodprice" id="prodprice"><?php echo
114
$value['price'] ?></strong></td>
115
</div>
116
<td class="col-sm-1 col-md-1 text-center" ><strong id="prodtotal" class="prodtotal"></strong></td>
117
<td class="col-sm-1 col-md-1">
118
<button type="button" id="remove" name="remove" onclick="removecart(<?php echo
119
$value['cart_id'] ?>)"
120
class="btn btn-danger">
121
<span class="fa fa-remove"></span> Remove
122
</button></td>
123
</tr>
124
<?php endforeach ?>
125
<tr>
126
<td> </td>
127
<td> </td>
128
<td> </td>
129
<td><h5>Subtotal</h5></td>
130
<td class="text-right"><h5><strong class="subtotal"></strong></h5></td>
131
</tr>
132
<tr>
133
<td> </td>
134
<td> </td>
135
<td> </td>
136
<td><h5>Delivery Fee</h5></td>
137
<?php foreach ($fees as $value): ?>
138
<td class="text-right"><h5><strong id="fees" class="fees">
139
<?php echo $value['fee_int'] ?>%</strong></h5></td>
140
<?php endforeach ?>
141
142
</tr>
143
<tr>
144
<td> </td>
145
<td> </td>
146
<td> </td>
147
<td><h3>Total</h3></td>
148
<td class="text-right" id="total"><h3><strong id="total" class="total"></strong></h3></td>
149
</tr>
150
<tr>
151
<td> </td>
152
<td> </td>
153
<td> </td>
154
<td>
155
<button type="button" id="test" class="btn btn-warning">
156
<span class="fa fa-shopping-cart"><h5 class="countdown"></h5></span> Cancel Order
157
</button></td>
158
<td>
159
160
<a href="#"><button type="button" data-toggle="modal" id="btncheckout" data-
161
target="#exampleModalCenter"
162
class="btn btn-success" >
163
Checkout <span class="fa fa-play"></span></a></button>
164
165
<a href="#"><button type="button" onclick="finishorder()" id="btnfinish" hidden class="btn
166
btn-success "
167
>
168
Finish Order <span class="fa fa-play"></span></a>
169
</button></td>
170
</tr>
171
</tbody>
172
</table>
173
</div>
174
Advertisement
Answer
First, properly comment your code and indent your code so it is easy for anyone to read and understand.
Now to the answer:
Since you have multiple elements rendered in he DOM inside your php foreach
, when you select the elements using $('.prodtotal')
you get an array.
What you can do is,
JavaScript
1
4
1
$('.prodtotal').each (function () {
2
console.log($(this).text())
3
})
4