422,024 Members | 1,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,024 IT Pros & Developers. It's quick & easy.

Avoid duplicate value in PHP MySQL pagination with rand()

100+
P: 123
I have a pagination that load more when I click on the load more button, which I got from the below URL http://makitweb.com/load-more-results-with-jqueryajax-and-php/

Whenever I add RAND() it always duplicates the value.


index.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $rowperpage = 10;
  4.  
  5. $allcount_query = "SELECT count(*) as allcount FROM users order by id";
  6. $allcount_result = mysql_query($allcount_query);
  7. $allcount_fetch = mysql_fetch_array($allcount_result);
  8.  $allcount = $allcount_fetch['allcount'];
  9.  
  10. $query = "select * from users order by RAND() asc limit 0,$rowperpage ";
  11. $result = mysql_query($query);
  12. while($row = mysql_fetch_array($result)){
  13. ?>
  14.  
  15. <div><h1><?php echo $row['id']; ?></div>
  16.  
  17. <?php
  18. }
  19. ?>
  20.  
  21. <h1 class="load-more">Load More</h1>
  22. <input type="hidden" id="row" value="0">
  23. <input type="hidden" id="all" value="<?php echo $allcount; ?>">

getData.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $row = $_POST['row'];
  3. $rowperpage = 10;
  4.  
  5.  
  6. $query = 'SELECT * FROM users ORDER BY id limit '.$row.','.$rowperpage;
  7. $result = mysql_query($query);
  8.  
  9. $html = '';
  10. while($row = mysql_fetch_array($result)){
  11.  
  12. $html .= '<div><h1>'.$id.'</h1></div>';
  13.  
  14. }
  15. ?>
Nov 21 '17 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 872
"Whenever I add RAND() it always duplicates the value."
Which value is duplicated?

Also about line #5, there is no need to do an 'order by' here, because the COUNT will not be different if it's ordered or not ....
Dec 6 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.