I am currently building a stock market simulator and, on my main stock market menu, I have 15 'Buy' buttons for each individual company. I have generated the 15 buttons and stock names using the following code:
HTML and PHP:
for ($a = 0; $a < $length; $a++)
{
echo "<tr>";
echo "<td style = 'text-align:center;font-size:15pt'>" . $companyNames[$a] . "</td>";
echo "<td style = 'text-align:center;font-size:15pt'>" . $companyTickers[$a] . "</td>";
?> <td style = "text-align:center;font-size:15pt" id = "<?php echo $priceIdentifiers[$a]; ?>"></td>
<td style = "text-align:center;font-size:15pt" id = "<?php echo $lowPriceIdentifiers[$a]; ?>"></td>
<td style = "text-align:center;font-size:15pt" id = "<?php echo $highPriceIdentifiers[$a]; ?>"></td>
<td style = 'text-align:center;font-size:15pt'><button id = "<?php echo $a; ?>" onclick = "myFunction()" style = 'font-size:12pt'><strong>BUY</strong></button></td>
<?php echo "</tr>";
}
?>
Where the value of $length = 15. I have further created a function in Javascript in an attempt to display the unique ID that was generated for each button (from $a) using the following code:
Javascript:
<script>
var a = <? echo json_encode($a); ?>;
function myFunction(){
window.alert(a);
}
</script>
When I am pressing the buttons using the above code, every time I press a button it increments the value that is displayed to me each time (so for 5 button presses, the display is "5", for 8 it is "8").
When looking at an approach on a different post: JavaScript - onClick to get the ID of the clicked button, I included some of the code in the post and to test my results, I clicked on several random buttons and was greeted with "15" every time.
My target is for the program to display "1" when I press the top button or "4" when I press the fourth button and so on. What can I go about doing to fix this problem?