Is is possible to increment javascript variables. I have a table that gets information from a database which then displays an edit option, once clicked it shows the user and then a drop down for a bunch of products to assign to that user. Once the user clicks on a product then it displays the price of that product. Now each entry needs to have a unique ID, which i succeeded by incrementing the ID's in the script, however the variables also need to change, this will eliminate me having to type out the script each time.
For this case here is the script and what i have tried. Just not successful on incrementing the variable names.
Note that the ID's gets populates with the ID's from the database. So it would be product_info1, product_info2 and the price would be price1, price2 and so on. Just the program does not want to work with the same variable names.
HTML
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Phone Number</th>
<th>Gender</th>
</tr>
<?php
$sql = "SELECT * FROM test_table";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["surname"]; ?></td>
<td><?php echo $row["phone_number"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><ul class="t-dropdown-list">
<a data-toggle="modal" data-target="#SetProductCustomer<?= $row["id"]; ?>"><li class="t-dropdown-item">Edit</li></a>
</ul></td>
</tr>
<div class="modal fade" id="SetProductCustomer<?= $row["id"]; ?>" class="" tabindex="-1" role="dialog" aria- labelledby="myModalLabel" aria-hidden="true">
<div class="modal-content">
<a href="test.php"><span class="close">×</span></a>
<p><?php echo $row['name']; ?> </p>
<select style="width: 100%;" name="" id="product_info<?= $row["id"]; ?>" class="browser-default custom-select-new">
<?php
$records = mysqli_query($db, "SELECT * FROM products");
while ($data = mysqli_fetch_array($records)) {
echo '<option value="' . $data['product_name'] . '"
data-price="' . $data['price'] . '" >'
. $data['product_name'] . '</option>';
}
?>
</select>
<input type="text" name="price" id="price<?= $row["id"]; ?>"/>
</div>
</div>
<?php
}
}
?>
<script>
for(var i=1; i < 4; i++){
var mySelect = mySelect + i;
var myNewOption = myNewOption + i;
mySelect = document.getElementById("product_info" + i);
mySelect.addEventListener("change", function() {
myNewOption = mySelect.options[mySelect.selectedIndex].getAttribute("data-price");
document.getElementById("price" + i).value = myNewOption;
});
}
</script>