Avoid duplicate value in PHP MySQL pagination with rand()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simon2x1
    New Member
    • Dec 2008
    • 123

    Avoid duplicate value in PHP MySQL pagination with rand()

    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

    Code:
    <?php
    
    $rowperpage = 10;
    
    $allcount_query = "SELECT count(*) as allcount FROM users order by id";
    $allcount_result = mysql_query($allcount_query);
    $allcount_fetch = mysql_fetch_array($allcount_result);
     $allcount = $allcount_fetch['allcount'];
    
    $query = "select * from users order by RAND() asc limit 0,$rowperpage ";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)){
    ?>
    
    <div><h1><?php echo $row['id']; ?></div>
    
    <?php
    }
    ?>
    
    <h1 class="load-more">Load More</h1>
    <input type="hidden" id="row" value="0">
    <input type="hidden" id="all" value="<?php echo $allcount; ?>">

    getData.php

    Code:
    <?php
    $row = $_POST['row'];
    $rowperpage = 10;
    
    
    $query = 'SELECT * FROM users ORDER BY id limit '.$row.','.$rowperpage;
    $result = mysql_query($query);
    
    $html = '';
    while($row = mysql_fetch_array($result)){
    
    $html .= '<div><h1>'.$id.'</h1></div>';
    
    }
    ?>
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    "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 ....

    Comment

    Working...