简体   繁体   中英

Php pagination and MYSQL Distinct

OK i have a basic php pagination script, which has a basic next and previous button. Now this works fine until i add a distinct clause. Please see code below.

$query = "SELECT COUNT(*) as num FROM $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
echo "$total_pages";
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
    $start = ($page - 1) * $limit; 
}else{
    $start = 0; 
    }   

// Get page data

$query1 = "SELECT Distinct model_group from $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;  
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);      
$LastPagem1 = $lastpage - 1;

Now if i change the query in query 1 to be

 $query = "SELECT * From

the code works fine, below is the code for my next and previous buttons.

Previous Button

  if ($page > 1){
        echo "<a href='$targetpage?page=$prev&type=$type&manufacturer=$manufacturer&year=$year'><div class='previous'><img src='images/PrevButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
    }else{
        echo "<span class='disabled'><div class='previous'>Previous</span></span>"; }

Next Button


if ($page < $lastpage){ 
        echo "<a href='$targetpage?page=$next&type=$type&manufacturer=$manufacturer&year=$year'><div class='next'><img src='images/MoreButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
    }else{
        echo "<span class='disabled'><div class='next'>More</span></span>";
    }

IS there anyway i can include the distinct value in to the count query? as i think it returns a different value to the second query.

i think you have to use a

SELECT COUNT(DISTINCT *)

in your first statement

a better way in mysql is to fix your paging problem with the SQL_CALC_FOUND_ROWS option in your select:

SELECT SQL_CALC_FOUND_ROWS * FROM mysql.user LIMIT 1

your result will be 1 row... but with

SELECT FOUND_ROWS();

you become the count of your result without the limit!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM