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>пропустить-&gt;</button> <input type="button" value="Выполненно" onclick="alert('Молодец!');javascript:window.location='index‌​.php'"> </form> </body> </html> 
  • one
    Can the code show who does that to you? And that would not be repeated - the only way to remember the last few that they actually do not choose. ru.stackoverflow.com/questions/21904/… - Mike
  • @Mike <? Php include_once 'db_connection.php'; ?> <! DOCTYPE html> <html lang = "en"> <head> <meta charset = "UTF-8"> <title> Document </ title> </ head> <body> <? Php include_once 'header. php '; ?> <? 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> skip -> </ button> <input type = "button" value = "Done" onclick = "alert ('Well done!'); javascript: window.location = 'index.php'" /> </ form> </ body> </ html> - Alexander
  • you need to add something like this to the array and mix it in, but I still don’t understand how ( - Alexander
  • it is necessary to store the array somewhere between calls - this is extremely inconvenient. it is easier to save the ID of the records that have already been shown and the request to finish a little 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) - Mike
  • Come to your senses! ORDER 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 make SELECT COUNT(id) , then make $rand=mt_rand(0,$count-1); and then select 1 entry via ORDER BY id LIMIT $rand, 1 - Sanya_Zol

2 answers 2

 <!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <?php include_once 'header.php'; include_once 'db_connection.php'; 

mysql is a deprecated extension and is no longer supported. Use mysqli or PDO

  mysql_query("SET NAMES 'utf8'"); 

Since we only need the value field, it is unnecessary to use *

  $result = mysql_query("SELECT value FROM men_challange"); 

Fill the array with quotes

  while($row=mysql_fetch_row($result)){ $values[] = $row[0]; } 

We mix

  shuffle($values); ?> <span id='cite'></span> <script type='text/javascript'> var container = document.getElementById('cite'); 

Create an array with quotes in the context of javascript

  var cites = <?php echo json_encode($values)?>; var index = 0; 

The replacement quote function in the <span id='cite'></span> block

  function changeCite(){ container.innerHTML = cites[index]; index++; 

If the quotes are over - go back to the very first

  if(index==cites.length)index=0; } 

Run once when the page loads

  changeCite(); </script> 

form is not needed in this case. Reloading the page with each press is also not needed.

  <button onclick='changeCite();'>пропустить-&gt;</button> </body> </html> 

    I will offer two options:

    1. make only one request to the server, during which an entire large array of quotes is loaded onto the page. Randomly mixed on the server, or already in the page using Javascript . By clicking on the button, the page is not loaded, but only displays the next quote from the array.
    2. “Remember” the id previous N quotations that have fallen out, and load the next page with a POST request, in which the list of N quotes of the previous quotations is transmitted. They are used in the request for an exception (as suggested in Mike’s comments).

    Since the question is about PHP, I’ll attach sample code for the second:

     <!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <?php $used = []; $where = ''; if( isset($_POST['used']) && is_array($_POST['used'])) { $used = array_map( $_POST['used'], function( $el){ return intval($el);}) $where = sprintf(' WHERE id NOT IN (%s) ', implode(',', $used)); } include_once 'db_connection.php'; include_once 'header.php'; mysql_query("SET NAMES 'utf8'"); $result = mysql_query("SELECT id, value FROM men_challange " . $where . " ORDER BY RAND() LIMIT 1"); $row = mysql_fetch_array($result); echo $row['value']; // вывод цитаты array_push( $used, intval( $row['id'])); $used = array_slice( $used, -5); // оставляем не более 5 последних id ?> <form method="post"> <?php foreach($used AS $id) { printf( '<input type="hidden" name="used[]" value="%d">', $id ); } ?> <button type="submit">ещё цитат!</button> </form> </body> </html>