I built a simple database where the page displays a random selection of quotes which are displayed in an unordered list. A js script cycles through each list item showing them one at a time.
This is the query loop:
while ($row = mysql_fetch_assoc($query)) {
echo "<li><span id='id'>";
// print the quote number
printf ("%s", $row['id']);
echo "</span> <span id='quote'>";
// print the quote
printf ("%s", $row['strategy']);
echo "</span> <span id='author'>";
// print the quote author
printf ("by %s", $row['author']);
echo "</span></li>";
}
So far so good. My problem is how to show the number of likes and dislikes, and have users ‘vote’ on them. I cannot figure out how I can keep array data active once it is printed out like this. Could I use a counter to keep track of row elements in PHP so I can then know which quote the user is voting on?
Thanks!
Your code was hard to read so I rewrote it. You want to use the quote id number to access other data about the quotes. Dhruv’s example was a good way to use an element’s id and get the data with javascript. But for some things you might want to handle more of it on the server.
You can see that the like link is just a link that passes the quote’s id as a $_GET variable. For the number of likes part assume that we already have that information and it’s already in an array with the index keys being the quote ids. (That’s the $like_array)
This is just the basic idea, but should get you going in the right direction.