programing

PHP를 이용한 페이지 수정 방법

javajsp 2023. 9. 26. 22:00

PHP를 이용한 페이지 수정 방법

안녕하세요 여러분 저는 아래 형식으로 제 페이지를 만들려고 합니다.

3페이지로 이동 [text-box] 이전 1 2 3 4 5 ..400 Next

그리고 아래는 나의 코드입니다.

             if(isset($_REQUEST['limit_start']) && $_REQUEST['limit_start'] > 0) $limit_start = $_REQUEST['limit_start'];
        else  $limit_start = 0 ;
        if(isset($_REQUEST['results_to_show']) )  $results_to_show= $_REQUEST['results_to_show'];
        else $results_to_show = 100;

        if(($limit_start-$results_to_show)>=0)
        $pagination.='<a href="details-inventory.php?limit_start='.($limit_start-$results_to_show).'&&results_to_show='.$results_to_show.'" >Previous</a> | ';

          if (isset($_REQUEST['submit']) && $_REQUEST['submit'] != "")$search_limited = 1;
          else $search_limited = 0;

        global $wpdb;
        $sql='SELECT count(*) 
        FROM `inventory_location` ';

        $data= $wpdb->get_results($sql,ARRAY_A);
         $row_count= $data[0]['count(*)']; 

        for($number=(($limit_start/$results_to_show)+1);$number<($row_count/$results_to_show);$number++)//($row_count/$results_to_show);$number++)($limit_start/$results_to_show)+
        $pagination.= '<a href="details-inventory.php?limit_start='.($number*$results_to_show).'&&results_to_show='.$results_to_show.'" >'.$number.'</a> | ';

        $pagination.= ' <a href="details-inventory.php?limit_start='.($limit_start+$results_to_show).'&&results_to_show='.$results_to_show.'" >Next </a> <br />';

이제 문제는...1페이지부터 마지막 페이지까지 모든 숫자를 보여줍니다... 아래 스타일로 깨트리고 싶습니다.

   1 2 3 4 5 ....400

고맙습니다

WordPress를 사용하는 경우 내장된 기능이 있습니다.paginate_links호출용으로

많은 정보를 여기 http://codex.wordpress.org/Function_Reference/paginate_links 에서 보실 수 있습니다.

나만의 기능을 만드는 것보다는 필요한 기능을 하는 내장 기능을 사용하는 것이 좋을 것입니다.

이것은 당신의 필요에 맞게 도움을 줄 것입니다.정확한 주장만 전달하면 됩니다.mid_size현재 페이지를 포함하지 않고 현재 페이지 양쪽에 표시할 페이지 번호를 지정해야 하는 인수입니다.

업데이트:

코드화는 아래와 같이 단순화할 수 있습니다.

global $wpdb;

//get the total count
$total_count = $wpdb->get_var('SELECT count(*) FROM `inventory_location`');

//number of results to be shown per page
$results_to_show_per_page   = 100;

//specify the number of page numbers to be shown on each side of the current page
$mid_size = 3;

//check whether the query argument page is set and get the current page
if (isset($_GET['page']))
    $page = abs((int)$_GET['page']);
else
    $page = 1;

//generate page links
$pagination_links = paginate_links( array(
                            'base' => add_query_arg( 'page', '%#%' ),
                            'total' => ceil($total_count / $results_to_show_per_page),
                            'current' => $page,
                            'mid_size'=> $mid_size 
                    ));


echo $pagination_links;

도움이 되길 바라요 :)

언급URL : https://stackoverflow.com/questions/16010088/how-to-fix-pagination-using-php