October 4th, 2012, 11:06 PM
-
Loop generated array of javascript buttons
Hello,
Thank you to anyone who can give me some ideas with this issue:
I have a MySQL database table with two columns, a name and a counter. I've written php scripts which will print each row (name and counter) line by line to a page and another script which increments the counter for a given row.
I want to be able to place a button next to each row which will increment the counter for that row on click. So I guess two main problems:
1) How to implement a for loop to place a button on each line with the termination condition being the number of rows in my db.
2) How to call the 'increment counter' script from a button with the correct row being incremented
I'm not looking for a canned solution, but rather any general guidance, resources, samples, you can provide.
Thanks,
Adam
October 5th, 2012, 03:37 PM
-
So you have something to the following?
php Code:
// mysql connection code
[…]
$results = mysql_query("SELECT `id`,`name`,`counter` FROM `TABLE_NAME`");
echo "<ul>";
while ($row = mysql_fetch_array($results)) { ?>
<li>
Name: <?php echo $row["name"]; ?> <br />
Counter: <?php echo $row["counter"]; ?> <br />
<a href="increment.php?id=<?php echo $row["id"]?>">Increment by 1</a>
</li>
<?php }
echo "</ul>";
where increment.php would be your increment script and you would pass the id of the row along which then would be accessible in PHP via:
php Code:
$id = (int) $_GET["id"];
// increment code
[…]
and then you would look for the id in you table and increment the counter by one.
If you don't have an id column (as PRIMARY_INDEX/KEY): you should.
(You could also have the increment.php script be executed in the background using AJAX.)
I hope that helped a little.