Start New Discussion within our Web Development Community

page-news.php

<?PHP
/*--------------------------------------------------------------------------------------
@Desc       :   Simple and Cool Paging with PHP
@author     :   SachinKRaj - http://blog.sachinkraj.com
@updates    :   http://blog.sachinkraj.com/how-to-create-simple-paging-with-php-cs/
@Comments   :   If you like my work, please drop me a comment on the above post link. 
                Thanks!
---------------------------------------------------------------------------------------*/
    function check_integer($which) {
        if(isset($_REQUEST[$which])){
            if (intval($_REQUEST[$which])>0) {
                //check the paging variable was set or not, 
                //if yes then return its number:
                //for example: ?page=5, then it will return 5 (integer)
                return intval($_REQUEST[$which]);
            } else {
                return false;
            }
        }
        return false;
    }//end of check_integer()

    function get_current_page() {
        if(($var=check_integer('page'))) {
            //return value of 'page', in support to above method
            return $var;
        } else {
            //return 1, if it wasnt set before, page=1
            return 1;
        }
    }//end of method get_current_page()

    function doPages($page_size, $thepage, $query_string, $total=0) {
        
        //per page count
        $index_limit = 10;

        //set the query string to blank, then later attach it with $query_string
        $query='';
        
        if(strlen($query_string)>0){
            $query = "&amp;".$query_string;
        }
        
        //get the current page number example: 3, 4 etc: see above method description
        $current = get_current_page();
        
        $total_pages=ceil($total/$page_size);
        $start=max($current-intval($index_limit/2), 1);
        $end=$start+$index_limit-1;

        echo '<br /><br /><div class="paging">';

		// Previous <<
        if($current==1) {
            echo '<span class="prn">&lt; < </span>&nbsp;';
        } else {
            $i = $current-1;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'">&lt; < </a>&nbsp;';
            echo '<span class="prn">...</span>&nbsp;';
        }

        if($start > 1) {
            $i = 1;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
        }

        for ($i = $start; $i <= $end && $i <= $total_pages; $i++){
            if($i==$current) {
                echo '<span>'.$i.'</span>&nbsp;';
            } else {
                echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
            }
        }

        if($total_pages > $end){
            $i = $total_pages;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
        }

		// Next >>
        if($current < $total_pages) {
            $i = $current+1;
            echo '<span class="prn">...</span>&nbsp;';
            echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'"> > &gt;</a>&nbsp;';
		// Next	>>	
        } else {
            echo '<span class="prn"> > &gt;</span>&nbsp;';
        }
        
        //if nothing passed to method or zero, then dont print result, else print the total count below:
        if ($total != 0){
            //prints the total result count just below the paging
            echo '<p id="total_count">(total '.$total.' results)</p></div>';
        }
        
    }//end of method doPages()
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>How to create simple cool paging with PHP and CSS</title>
</head>
<style type="text/css">
/*---Lets style paging to make it look more cool--*/
     
     /*--example specific styling: you dont need it really in your script, it is just to make this example look good---*/
        body { font-family: Helvetica, arial, sans-serif; font-size:13px; font-weight: normal;}
        
     /*---Paging specific styling----*/     
        .paging { padding:10px 0px 0px 0px; text-align:center; font-size:13px;}
        .paging.display{text-align:right;}
        .paging a, .paging span {padding:2px 8px 2px 8px; font-weight :normal}
        .paging span {font-weight:bold; color:#000; font-size:13px; }
        .paging a, .paging a:visited {color:#000; text-decoration:none; border:1px solid #dddddd;}
        .paging a:hover { text-decoration:none; background-color:#6C6C6C; color:#fff; border-color:#000;}
        .paging span.prn { font-size:13px; font-weight:normal; color:#aaa; }
        .paging a.prn, .paging a.prn:visited { border:2px solid #dddddd;}
        .paging a.prn:hover { border-color:#000;}
        .paging p#total_count{color:#aaa; font-size:12px; font-weight: normal; padding-top:8px; padding-left:18px;}
        .paging p#total_display{color:#aaa; font-size:12px; padding-top:10px;}
</style>


<?php get_header(); ?>


<div id="body">
<div id="content">
	<div id="main">
	<h4>LOREM IPSUM - <font color="red"><?php the_time('d'); ?>&nbsp<?php the_time('M'); ?>&nbsp<?php the_time('Y'); ?></font></h4>
 		<div class="post">

        
	<?php if (have_posts()) : ?>
		<?php while (have_posts()) : the_post(); ?>
        
        <p style="border-bottom: 2px dotted #FF0000; width: 900px;"></p><br />			
                      
            <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/pic1.png" ALT="picture1" ALIGN=LEFT><div id="ptitle"><?php the_title(); ?><font color="black"> - <?php the_time('d'); ?>&nbsp<?php the_time('M'); ?>&nbsp<?php the_time('Y'); ?></font></div>
            
            <p><?php the_content(''); ?>
         
         
        <?PHP
        //This is the actual usage of function, It prints the paging links 
		// deleted: category=sports
		
        doPages(15, 'page-news.php', '', 85); 
        ?>
      	
     <?php endwhile; ?>
        
<?php endif; ?>
       
  
    </div>
   </div>



<?php get_sidebar(); ?>
 
 </div>
</div>

<?php get_footer(); ?>

Hi, I am using a freeware paging and customizing according to my needs. Can anyone help me in setting the next page ?

http://ocklaw.com/new/news/

If you press [2], the next page, it will carries you to

http://ocklaw.com/new/news/page-news.php?page=2

How am I suppose to set the next page ? How ?

At this point, I am confuse where is the next page come from ?

It looks like you're using WordPress. If so, can you not just use the built in pagination functionality?

<?php global $wp_query; ?>
<?php if($wp_query->max_num_pages > 1): ?>
    <footer>
        <p class="pagination">
            Pages:
	    <?php
	        $big = 999999999; // need an unlikely integer
		$pagination = paginate_links(array(
		    'base' => str_replace($big, '%#%', get_pagenum_link($big)),
                    'format' => '?paged=%#%',
		    'current' => max(1, get_query_var('paged')),
		    'total' => $wp_query->max_num_pages,
		    'end_size' => 3,
		    'mid_size' => 5,
		    'type' => 'array',
		));
			
		echo implode('|', $pagination);
    	    ?>
	</p>
    </footer>
<?php endif; ?>

I copy paste the and thought that the pagination would come automatically. It doesn't now, the page turn into chaoz after entering the codes. Why is it? What else should I set up ?

Well, if anyone can help me answering my first post or replying the second alternative solution ?

Well it largely depends how you're finding the content you want to paginate. Are you using WP_Query?

And it's probably turned the page into chaoz because the content isn't styled.

Can you post the where you're retrieving the items you want to paginate and provide more information?

http://ocklaw.com/new/news/

Well, the part that's turn into chaos is still in localhost. Yet, that's the link before I add the wordpress pagination.

I add the wordpress pagination just next to my original pagination.

I think you can browse my codes and suggest me where should I place the wordpress pagination or what else should I add to the style css.

page-news.php

<?PHP
/*--------------------------------------------------------------------------------------
@Desc       :   Simple and Cool Paging with PHP
@author     :   SachinKRaj - http://blog.sachinkraj.com
@updates    :   http://blog.sachinkraj.com/how-to-create-simple-paging-with-php-cs/
@Comments   :   If you like my work, please drop me a comment on the above post link. 
                Thanks!
---------------------------------------------------------------------------------------*/
    function check_integer($which) {
        if(isset($_REQUEST[$which])){
            if (intval($_REQUEST[$which])>0) {
                //check the paging variable was set or not, 
                //if yes then return its number:
                //for example: ?page=5, then it will return 5 (integer)
                return intval($_REQUEST[$which]);
            } else {
                return false;
            }
        }
        return false;
    }//end of check_integer()

    function get_current_page() {
        if(($var=check_integer('page'))) {
            //return value of 'page', in support to above method
            return $var;
        } else {
            //return 1, if it wasnt set before, page=1
            return 1;
        }
    }//end of method get_current_page()

    function doPages($page_size, $thepage, $query_string, $total=0) {
        
        //per page count
        $index_limit = 10;

        //set the query string to blank, then later attach it with $query_string
        $query='';
        
        if(strlen($query_string)>0){
            $query = "&amp;".$query_string;
        }
        
        //get the current page number example: 3, 4 etc: see above method description
        $current = get_current_page();
        
        $total_pages=ceil($total/$page_size);
        $start=max($current-intval($index_limit/2), 1);
        $end=$start+$index_limit-1;

        echo '<br /><br /><div class="paging">';

		// Previous <<
        if($current==1) {
            echo '<span class="prn">&lt; < </span>&nbsp;';
        } else {
            $i = $current-1;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'">&lt; < </a>&nbsp;';
            echo '<span class="prn">...</span>&nbsp;';
        }

        if($start > 1) {
            $i = 1;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
        }

        for ($i = $start; $i <= $end && $i <= $total_pages; $i++){
            if($i==$current) {
                echo '<span>'.$i.'</span>&nbsp;';
            } else {
                echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
            }
        }

        if($total_pages > $end){
            $i = $total_pages;
            echo '<a href="'.$thepage.'?page='.$i.$query.'" title="go to page '.$i.'">'.$i.'</a>&nbsp;';
        }

		// Next >>
        if($current < $total_pages) {
            $i = $current+1;
            echo '<span class="prn">...</span>&nbsp;';
            echo '<a href="'.$thepage.'?page='.$i.$query.'" class="prn" rel="nofollow" title="go to page '.$i.'"> > &gt;</a>&nbsp;';
		// Next	>>	
        } else {
            echo '<span class="prn"> > &gt;</span>&nbsp;';
        }
        
        //if nothing passed to method or zero, then dont print result, else print the total count below:
        if ($total != 0){
            //prints the total result count just below the paging
            echo '<p id="total_count">(total '.$total.' results)</p></div>';
        }
        
    }//end of method doPages()
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>How to create simple cool paging with PHP and CSS</title>
</head>
<style type="text/css">
/*---Lets style paging to make it look more cool--*/
     
     /*--example specific styling: you dont need it really in your script, it is just to make this example look good---*/
        body { font-family: Helvetica, arial, sans-serif; font-size:13px; font-weight: normal;}
        
     /*---Paging specific styling----*/     
        .paging { padding:10px 0px 0px 0px; text-align:center; font-size:13px;}
        .paging.display{text-align:right;}
        .paging a, .paging span {padding:2px 8px 2px 8px; font-weight :normal}
        .paging span {font-weight:bold; color:#000; font-size:13px; }
        .paging a, .paging a:visited {color:#000; text-decoration:none; border:1px solid #dddddd;}
        .paging a:hover { text-decoration:none; background-color:#6C6C6C; color:#fff; border-color:#000;}
        .paging span.prn { font-size:13px; font-weight:normal; color:#aaa; }
        .paging a.prn, .paging a.prn:visited { border:2px solid #dddddd;}
        .paging a.prn:hover { border-color:#000;}
        .paging p#total_count{color:#aaa; font-size:12px; font-weight: normal; padding-top:8px; padding-left:18px;}
        .paging p#total_display{color:#aaa; font-size:12px; padding-top:10px;}
</style>


<?php get_header(); ?>


<div id="body">
<div id="content">
	<div id="main">
	<h4>LOREM IPSUM - <font color="red"><?php the_time('d'); ?>&nbsp<?php the_time('M'); ?>&nbsp<?php the_time('Y'); ?></font></h4>
 		<div class="post">

    <?php query_posts('posts_per_page=5'); ?>
        
	<?php if (have_posts()) : ?>
		<?php while (have_posts()) : the_post(); ?>
        
        <p style="border-bottom: 2px dotted #FF0000; width: 900px;"></p><br />			
                      
            <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/pic1.png" ALT="picture1" ALIGN=LEFT><div id="ptitle"><?php the_title(); ?><font color="black"> - <?php the_time('d'); ?>&nbsp<?php the_time('M'); ?>&nbsp<?php the_time('Y'); ?></font></div>
            
            <p><?php the_content(''); ?>
         
                    	
     <?php endwhile; ?>
        
      <?PHP
       //This is the actual usage of function, It prints the paging links 
	   //deleted: category=sports
		
       doPages(15, 'page-news.php', '', 85); 
      ?>  
        
        
 
	</p>
    </footer>
    
    
<?php endif; ?>  
        
<?php // endif; ?>
       
  
    </div>
   </div>


<?php
/*
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
	'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
	'format' => '?paged=%#%',
	'current' => max( 1, get_query_var('paged') ),
	'total' => $wp_query->max_num_pages
) );
*/
?>

<?php get_sidebar(); ?>
 
 </div>

Try replacing line 136 with:

$page = max(1, get_query_var('page'));
query_posts("posts_per_page=5&paged={$page}");

Try replacing line 136 with:

$page = max(1, get_query_var('page'));
query_posts("posts_per_page=5&paged={$page}");

Thanks. It works. but could you explain what each code means ?

I'm glad it solved your problem. Please mark the thread as solved.

And here's a very simple explanation:

// Select maximum value - 1 or the current page
$page = max(1, get_query_var('page'));
// Create a new WP_Query and pass the current page value as an argument
query_posts("posts_per_page=5&paged={$page}");
This article has been dead for over six months. Start a new discussion instead.