This code counts 'clicks' for only first Product in the list fetch from the database but how they can produce 'clicks count' for Remaining products
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = '';
$connect=mysqli_connect("localhost","root","","yii2_advanced");
$query = mysqli_query($connect,"SELECT * FROM product");
$dynamicList ="";
WHILE ($rows = mysqli_fetch_array($query)):
$idr = $rows['product_id'];
$product_name = $rows['product_name'];
$product_amount = $rows['product_amount'];
$product_type = $rows['product_type'];
$dynamicList .= ' <script type="text/javascript">
var clicks = 0;
function onClick() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
};
function onClick2(){
if(clicks >0){
clicks -= 1;
document.getElementById("clicks").innerHTML = clicks;
}
else if(clicks == 0){
document.getElementById("clicks").innerHTML = clicks;
}
};
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="%" valign="top"><img style="border:#666 1px solid;" src="'. $idr . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></td>
<td width="83%" valign="top">' . $product_name . '<br />
Rs.' . $product_amount . '<br />
<button onClick="onClick2()" type="button"><b>-</b></button>
<p style="display:inline-block;"><a id="clicks">0</a></p>
<button onClick="onClick()"><b>+</b></button>
</td>
</tr>
</table>';
endwhile;
?>
For better illustration I will show you image below:

jquerythen usedocument.getElementById. 2) wire up your events via jquery instead ofonclick=, that way you get athis. 3) use classes instead of IDs 4) store the click count on the element, not as a single global 5) usethisto get the relevant clicks box$(this).closest("td").find("a").text(clicks)