Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2019
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Regarding Pagination PLEASE HELP!

    Hi,

    I am having this php code for the pagination :-

    I WANT TO HAVE IT LIKE THIS << 1 2 3 4 5 >> [ OR ] PREVIOUS 1 2 3 4 5 NEXT . HOW CAN I DO THIS?

    Code:
    $sqlcount = "SELECT COUNT(id) FROM products";
    
    $rs_result = mysqli_query($db, $sqlcount);
    $rowcount = mysqli_fetch_array($rs_result);
    $total_records = $rowcount[0];
    //echo $total_records;
    $total_pages = ceil($total_records / $limit);
    $pagLink = "<ul class='pagination' style='float:right;'>";
    for ($i=1; $i<=$total_pages; $i++) {
                 $pagLink .= "<li><a href='https://clysarts.com/admin/productlist1.php?page=".$i."'>".$i."</a></li>";
    };
    echo $pagLink . "</ul>";
    
    ?>
    Please let me know. So will appreciate your time on this regard.


    Thanks

    Gary
    Last edited by vinyl-junkie; Jun 12th, 2019 at 05:06 PM. Reason: corrected code tags

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    West Des Moines, Iowa
    Posts
    8,104
    Thanks
    37
    Thanked 1,086 Times in 1,082 Posts
    It's better to use CSS <style> sections or a separate style sheet instead of inline code,but to make inline styles inside of your PHP(A real horrible idea) Use this:
    Code:
    $pagLink = "<ul class='pagination' style='float:right;'>";
    for ($i=1; $i<=$total_pages; $i++) {
        $pagLink .= "<li style=\"display:inline;\"><a href='https://clysarts.com/admin/productlist1.php?page=".$i."'>".$i ."</a></li>";
    };
    echo $pagLink . "</ul>";
    A better way
    HTML has

    Code:
    <style>
    .pagination{float:right;}
    .pagination li{display:inline;}
    </style>
    Wedding a few of the needed CSS:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title></title>
    <style>
    .pagination{float:right;}
    .pagination li{
        display:inline;
        margin-right: 10px;
    }
    .pagination li a{
        text-decoration: none;
    }    
    </style>
    </head>
        
    <body>
    <?php
    $total_records = 5;
    $limit = 1;
    
    $total_records = 5;
    //echo $total_records;
    $total_pages = ceil($total_records / $limit);
    echo $total_pages.'<br>';
    		
    		
    $pagLink = "<ul class='pagination'>";
    for ($i=1; $i<=$total_pages; $i++) {
        $pagLink .= "<li><a href='https://clysarts.com/admin/productlist1.php?page=".$i."'>".$i ."</a></li>";
    };
    echo $pagLink . "</ul>";
    ?> 
    </body>
    </html>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. Users who have thanked sunfighter for this post:

    gary4u (Jun 14th, 2019)

  4. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,497
    Thanks
    4
    Thanked 499 Times in 487 Posts
    1) stop making variables for nothing.

    2) stop putting static style in the markup.

    3) If you're just going to echo it, don't get strings involved.

    Here, try mine.

    Code:
    function template_pagination($urlRoot, $current, $perPage, $totalItems) {
    
    	if ($totalItems > $perPage) {
    	
    		echo '
    		<div class="pagination">
    			Pages:
    			<ul>';
    			
    		if (($currentPage = floor($current / $perPage)) > 0) echo '
    				<li><a href="', $urlRoot, '0">First</a></li>
    				<li><a href="', $urlRoot, $currentPage - 1, '" title="previous" rel="previous">&laquo;</a></li>';
    		
    		if (($lastPage = floor(($totalItems - 1) / $perPage)) > 9) {
    			$halfPer = ceil($perPage / 2);
    			$counter = ($currentPage <= $halfPer) ? 0 : $currentPage - $halfPer;	
    			if (($endPage = $counter + $perPage) > $lastPage) {
    				$endPage = $lastPage;
    				$counter = $endPage - $perPage;
    			}
    		} else {
    			$counter = 0;
    			$endPage = $lastPage;
    		}
    	
    		while ($counter <= $endPage) {
    			echo '
    				<li>', (
    					($noAnchor = ($counter == $currentPage)) ?
    					'<span>' :
    					'<a href="' . $urlRoot . $counter . '">'
    				), ++$counter, (
    					$noAnchor ?
    					'</span>' :
    					'</a>'
    				), '</li>';
    		}
    		
    		if ($currentPage < $lastPage) echo '
    				<li><a href="', $urlRoot, ++$currentPage, '" title="next" rel="next">&raquo;</a></li>
    				<li><a href="', $urlRoot, $lastPage, '">Last</a></li>';
    		
    		echo '
    			</ul>
    		<!-- .pagination --></div>';
    			
    	}
    	
    } // template_pagination
    If you were to call it as:

    template_pagination('page.php?page=', 0, 5, 10);

    It will output:
    Code:
    		<div class="pagination">
    			Pages:
    			<ul>
    				<li><span>1</span></li>
    				<li><a href="page.php?page=1">2</a></li>
    				<li><a href="page.php?page=2">3</a></li>
    				<li><a href="page.php?page=3">4</a></li>
    				<li><a href="page.php?page=4">5</a></li>
    				<li><a href="page.php?page=1" title="next" rel="next">&raquo;</a></li>
    				<li><a href="page.php?page=10">Last</a></li>
    			</ul>
    		<!-- .pagination --></div>
    Providing more than enough hooks to style that however you like. It omits the first/prev links if you're on the first one, and the next/last links if you're on the last one. It even omits the list entirely if there aren't enough items to need pagination.

    For the query side of things:

    Code:
    $totalProducts = $db->query('
    	SELECT COUNT(id)
    	FROM products
    ')->fetch_row()[0];
    
    template_pagination(
    	'/admin/productlist1.php?page=',
    	(array_key_exists('page', $_POST) ? $_POST['page'] : 0), // current page
    	5, // number per page
    	$totalProducts
    );
    Nice part is that said template routine is so easily re-usable and handles all the divide/floor/ceil stuff for you.

    A sample CSS might go something like:

    Code:
    .pagination {
    	text-align:center;
    }
    
    .pagination ul {
    	list-style:none;
    	display:inline;
    }
    
    .pagination li {
    	display:inline;
    }
    
    .pagination a,
    .pagination span {
    	display:inline-block;
    	padding:0.5em;
    	text-decoration:none;
    	border:2px solid #CCC;
    	background:#EEE;
    	color:#000;
    }
    
    .pagination a {
    	border:2px solid #666;
    	background:#CCC;
    	box-shadow:inset 0 0.5em 0.5em #FFF;
    }
    
    .pagination a:active,
    .pagination a:focus,
    .pagination a:hover {
    	background:#ACE;
    }
    About what you're looking for?
    Last edited by deathshadow; Jun 12th, 2019 at 06:15 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  5. #4
    New Coder
    Join Date
    May 2019
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    Thank you for your response but this did not work.

    I have did it-

    $total_records = 5;
    $limit = 1;

    $total_records = 5;

    But

    What happens is that when onclick on a page number , it goes to next page and shows only 12345. And when you click the previous number it then shows the previous page but the complete number of pages, say it has 19 pages , it shows all 19 numbers. like 1234567891011 to 19.

    Thanks
    Gary
    Last edited by gary4u; Jun 14th, 2019 at 12:02 PM.

  6. #5
    New Coder
    Join Date
    May 2019
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    Can you insert the my code in - template_pagination , it will nice. As this website is live and would not like to screw it up.

    Thanks

    Gary


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •