I’m working on a bit of PHP/MySQL code that randomly outputs data selected from the database.
For example, my database table contains values for Months, Days, and Events.
I run this query:
SELECT event_id, month, day_num, event FROM mytable WHERE month = DATE_FORMAT(CURDATE(), '%M') AND day_num = DAY(CURDATE());
The query returns several results:
event_id: 1388
Month: ‘December’
Day: 12
Event: ‘Nothing major happened.’
event_id: 1389
Month: ‘December’
Day: 12
Event: ‘This was an uneventful day.’
event_id: 1390
Month: ‘December’
Day: 12
Event: ‘Peaceful and quiet’
event_id: 1391
Month: ‘December’
Day: 12
Event: ‘Same old.’
I want to output one of the results into a browser, and then click a button and output another result. I want to make sure that every time a button is clicked, the output in the browser is not the same as immediately before.
For example, if the event_id that is currently output is 1388, I want to make sure that when I click the button, the event_id 1388 doesn’t get loaded.
Would be grateful for a bit of expertly advice.
If you need it sequentially you can use the
limitasHere from is 1 for click, 2 for the second, 3 for the third and so on.
You will have this query execution in a method which will receive the
fromas a parameter.At your front end, when the page loads initialize a variable to
0.As per your question, when a user clicks on the button,
Set an onclick event on the button which will increment the variable by
1,Pass this variable to the method from where you are querying the database.
So for every click you will get a new record.
The from, 1 signifies that we need a single row from the rownumber.