Most recently, I began to study web programming, and I try to immediately apply knowledge in practice, but it is not always smooth, so the following question was formed (I apologize if it turns out to be very primitive):
the program should output a random quote from the table, and when you click a button, the quote should switch to the next random quote. Everything would be fine, but it often happens that the same quote is repeated 2-3 times, and you have to press a button several times in order for it to switch.
Page code (from comment):
<?php include_once 'db_connection.php'; ?> <!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <?php include_once 'header.php'; mysql_query("SET NAMES 'utf8'"); $result = mysql_query("SELECT * FROM men_challange ORDER BY RAND() "); $row = mysql_fetch_array($result); echo $row['value']; ?> <form> <button>пропустить-></button> <input type="button" value="Выполненно" onclick="alert('Молодец!');javascript:window.location='index.php'"> </form> </body> </html>
select * from man_challange WHERE ID not in(1,2,3) order by rand()
all - the query will select only the lines except those specified (1,2,3). And where to store ... for example in cookies or in session data (if any) - MikeORDER BY RAND()
is evil , a random value will be calculated for each line and sorted (in memory / swap) will be sorted. This will create a strong load on the base. It would be more correct to first makeSELECT COUNT(id)
, then make$rand=mt_rand(0,$count-1);
and then select 1 entry viaORDER BY id LIMIT $rand, 1
- Sanya_Zol