Ogranicz liczbę stron

$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("Test");

$strSQL = "SELECT * FROM UserAddedRecord WHERE (Name LIKE '%".$getname."%' and State LIKE '%".$getstate."%' and Cell LIKE '%".$getcell."%' and Custgroup LIKE '%".$getgroup."%') AND user_id=$id";

$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);

$Per_Page = 5;   

if (!isset($_GET['Page'])) {
    $Page = 1;
} else {
    $Page = $_GET['Page'];
}

$Prev_Page = $Page - 1;
$Next_Page = $Page + 1;

$Page_Start = (($Per_Page * $Page) - $Per_Page);
if ($Num_Rows <= $Per_Page) {
    $Num_Pages = 1;
} elseif (($Num_Rows % $Per_Page) == 0) {
    $Num_Pages = ($Num_Rows / $Per_Page) ;
} else {
    $Num_Pages = ($Num_Rows / $Per_Page) + 1;
    $Num_Pages = (int) $Num_Pages;
}


$strSQL .=" order  by addedrec_ID DESC LIMIT $Page_Start , $Per_Page";
$objQuery  = mysql_query($strSQL) or trigger_error(mysql_error());;

if ($Prev_Page) {
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'><< Back</a> ";
}

for ($i=1; $i <= $Num_Pages; $i++) {
    if ($i != $Page) {
        echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$i&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>$i</a> ";
    } else {
        echo "<b> $i </b>"; 
    }
}

if ($Page!=$Num_Pages) {
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>Next>></a> ";        
}

mysql_close($objConnect);

Jest to kod, którego używam do tworzenia stron do wyszukiwania danych.Ale po prostu zauważyłem, że mam zbyt wiele rekordów, to będzie miało zbyt wiele numerów stron.Staram się ograniczyć wyświetlany numer strony i szukać wielu przykładów ..ma wiele sposobów, aby to zrobić, ale nadal nie mam pojęcia, jak to ograniczyć dla mojej metody...

Author: The Codesee, 2011-12-02

7 answers

Ok jeśli masz na myśli Pokaż coś takiego

Prev 1 2 3 4 5 6 .. 40 41 Next 
Prev 1 2 .. 6 7 8 9 10 .. 40 41 Next

Pierwszą rzeczą, jaką musimy zrobić, to stworzyć funkcję, która przetworzy to, czego potrzebujemy do wyprowadzenia paginacji. Oto funkcja, której używam i działa dobrze.

function get_paging_info($tot_rows,$pp,$curr_page)
{
    $pages = ceil($tot_rows / $pp); // calc pages

    $data = array(); // start out array
    $data['si']        = ($curr_page * $pp) - $pp; // what row to start at
    $data['pages']     = $pages;                   // add the pages
    $data['curr_page'] = $curr_page;               // Whats the current page

    return $data; //return the paging data

}

Teraz ta funkcja jest dość solidna i działa bardzo dobrze dla mnie.

Więc przekazujesz tę funkcję

  • $tot_rows = liczone wiersze dla zapytania
  • $pp = pozycje na stronie
  • $curr_page = bieżący numer strony

Ok, teraz, gdy ty masz potrzebne dane, musisz je wyświetlić.

Oto, czego używam i proszę przeczytaj, zanim pomyślisz: "ah, to za długo". To bardzo proste.

Oto migawka tego, co zwróci

Tutaj wpisz opis obrazka

    <!-- Create the query -->
    <?php $count = mysql_fetch_assoc( mysql_query ( "SELECT COUNT( rows ) as count FROM table" ) ) ;

    <?php $count = $count[0]['count']; ?>

<!-- Call our function from above -->
<?php $paging_info = get_paging_info($count,5,34); ?>


<p>
    <!-- If the current page is more than 1, show the First and Previous links -->
    <?php if($paging_info['curr_page'] > 1) : ?>
        <a href='' title='Page 1'>First</a>
        <a href='' title='Page <?php echo ($paging_info['curr_page'] - 1); ?>'>Prev</a>
    <?php endif; ?>



    <?php
        //setup starting point

        //$max is equal to number of links shown
        $max = 7;
        if($paging_info['curr_page'] < $max)
            $sp = 1;
        elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
            $sp = $paging_info['pages'] - $max + 1;
        elseif($paging_info['curr_page'] >= $max)
            $sp = $paging_info['curr_page']  - floor($max/2);
    ?>

    <!-- If the current page >= $max then show link to 1st page -->
    <?php if($paging_info['curr_page'] >= $max) : ?>

        <a href='' title='Page 1'>1</a>
        ..

    <?php endif; ?>

    <!-- Loop though max number of pages shown and show links either side equal to $max / 2 -->
    <?php for($i = $sp; $i <= ($sp + $max -1);$i++) : ?>

        <?php
            if($i > $paging_info['pages'])
                continue;
        ?>

        <?php if($paging_info['curr_page'] == $i) : ?>

            <span class='bold'><?php echo $i; ?></span>

        <?php else : ?>

            <a href='' title='Page <?php echo $i; ?>'><?php echo $i; ?></a>

        <?php endif; ?>

    <?php endfor; ?>


    <!-- If the current page is less than say the last page minus $max pages divided by 2-->
    <?php if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2))) : ?>

        ..
        <a href='' title='Page <?php echo $paging_info['pages']; ?>'><?php echo $paging_info['pages']; ?></a>

    <?php endif; ?>

    <!-- Show last two pages if we're not near them -->
    <?php if($paging_info['curr_page'] < $paging_info['pages']) : ?>

        <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.($paging_info['curr_page'] + 1); ?>' title='Page <?php echo ($paging_info['curr_page'] + 1); ?>'>Next</a>

        <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.$paging_info['pages']; ?>' title='Page <?php echo $paging_info['pages']; ?>'>Last</a>

    <?php endif; ?>
</p>
 29
Author: Sean H Jenkins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-02 21:00:27

$count_pages = ceil ($total / $items_per_page);

 3
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-18 10:49:21

Rozwiązałem bardzo podobny problem dla kogoś innego z bardzo podobnym skryptem tutaj: Jak ograniczyć strony wyświetlane w skrypcie paginacji

Zauważyłem również kilka innych anomalii w Twoim skrypcie (poza nieczytelnym formatowaniem, które naprawiłem). Należy zmienić wszystkie wystąpienia $_SERVER[SCRIPT_NAME] na $_SERVER['SCRIPT_NAME']. W Twoim skrypcie, na przykład:

echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=...";

Stałoby się:

echo " <a href ='{$_SERVER['SCRIPT_NAME']}?Page=...";
 2
Author: Yes Barry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:09:45

Uprość to, oto kod:

<?php       
    //put this code on the begining of the page
    $page=$_GET['page']; //Getting page number from url | example: www.mydomain.com/index.php?page=3
    if(!is_numeric($page)) $page=1; //If there is no page number specified we set number 1
$queryCount = "SELECT * FROM table "; //select from db
$query = $DB->query( $queryCount);
    $num = mysql_num_rows ($query); //count num rows
    $per_page=5; //default 5 results per page
$start=$per_page*($page-1); //start for select on next page (page2, 3, 4,5)
$end=min($num,$page*$per_page); //end


//here is select for your results. Be careful for LIMIT in the select!
$query = "SELECT * FROM page LIMIT $start, $per_page ";


//page bottom, where you want to put your numbers
$pages=ceil($num/$per_page);
  for($s=1; $s<=$pages; $s++)
  {
    if($s==$page)
      $numPage  .= "[$s] ";
    else
      $numPage  .= "<a href='index.php?page=$s'>$s</a> ";
  }

echo $numPage;

?>

Nie testowałem, ale myślę, że musi działać: -)

Cindy, Ivan

 2
Author: Ivan Laharnar mink.si,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 16:49:54

Mój kod zawsze pokazuje co najmniej 9 stron w przypadku wszystkich stron>9 cokolwiek jesteś na stronie 1,2,3

<ul class="pagination pagination-sm" style="align:center">
    <li><a href="./?page=1"><</span></a></li>
    <?php
    if ($current_page <4){
        if ($total_pages<9)
        {
            for($i = 1; $i <=  $total_pages; $i++)
            {

                if ($i == $current_page)
                {
                    echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
                } else {
                    echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n";
                }
            }
        }
        else
        {
            for($i = 1; $i <=  9; $i++)
            {

                if ($i == $current_page)
                {
                    echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
                } else {
                    echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n";
                }
            }
        }
    }
    elseif ($current_page >$total_pages-4)
    {
        if ($total_pages<9)
        {
            for($i = 1; $i <=  $total_pages; $i++){

                if ($i == $current_page){
                    echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
                } else {
                    echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n";
                }
            }
        }else{
            for($i = $total_pages-8; $i <=  $total_pages; $i++){

                if ($i == $current_page){
                    echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
                } else {
                    echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n";
                }
            }
        }

    }else{
        for($i = max(1, $current_page - 4); $i <= min($current_page + 4, $total_pages); $i++){

            if ($i == $current_page){
                echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n";
            } else {
                echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n";
            }
        }
    }
    ?>
    <li>
        <a href="./?trang=<?php echo $total_pages ?>">></a>
    </li>
</ul>
 2
Author: NgoKien,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-22 06:32:34

Będziesz musiał użyć klauzuli LIMIT w swoim poleceniu sql. Na przykład:

SELECT <column> 
  FROM <table>
    LIMIT 0, 5

Reference: SELECT Syntax

 2
Author: Cyclonecode,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-29 17:11:50

Celem tego kodu jest tylko pokazanie określonej (pewnej) liczby stron w paginacji

Pobieramy bieżący numer strony z adresu url i definiujemy $curr_pg_numb

Pobranie liczby wszystkich stron z mysql i zdefiniowanie $total_pages

A oto kod do paginacji

$counter=0;
$init_num_to_left = 3;//initial number of pages to show to left from current page
$init_num_to_right = 4;

if( $curr_pg_numb - $init_num_to_left < 1 ){
$init_num_to_right = $init_num_to_right + ( 1 - ($curr_pg_numb - $init_num_to_left) );
$init_num_to_left = $curr_pg_numb - 1;
}
else if( $curr_pg_numb + $init_num_to_right > $total_pages ){
$init_num_to_left = $init_num_to_left + ( ($curr_pg_numb + $init_num_to_right) - $total_pages );
$init_num_to_right = ($curr_pg_numb + $init_num_to_right) - $total_pages;
}

while($counter <= $total_pages ) {
  if ( $counter > 0 ){
    if ($curr_pg_numb==$counter){
    echo '<strong> '. ( htmlspecialchars($curr_pg_numb , ENT_QUOTES, "UTF-8") + 0 ). '</strong> ';
    }
    else {
    if ( $counter >= ($curr_pg_numb - $init_num_to_left ) and $counter <= ($curr_pg_numb + $init_num_to_right ) ) {
    echo '<a href="?cpn='. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '" style="margin:5px;">'. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '</a>';
    }
    }//else {
  }//if ( $counter > 0 ){
$counter++;
}//while($counter <= $total_pages ) {
 1
Author: Andris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-20 13:28:00