Jump to content

ModRewrite pagination


lovephp

Recommended Posts

i have already got my category into seo url like site.com/category-name

 

now i have issue with the pagination i tried but its not taking any affect could one tell me what i am doing wrong?

 

my categories.php

 

<?php
require('includes/config.php');

$url = $_SERVER['REQUEST_URI'];
$url = str_replace('/blog/category/', '', $url);
echo $catid = getCatID($url);

$stmt = $db->prepare("SELECT * FROM blog_categories WHERE catUrl = :catUrl");
$stmt->bindParam(':catUrl', $url, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch();

//if post does not exists redirect user.
if($row['catUrl'] == ''){
    header('Location: ../');
    exit;
}

?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $row['catName']; ?></title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<?php echo DIR; ?>assets/css/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo DIR; ?>assets/css/font-awesome.min.css">
<link rel="stylesheet" href="<?php echo DIR; ?>assets/css/font.css">
<link rel="stylesheet" href="<?php echo DIR; ?>assets/css/animate.css">
<link rel="stylesheet" href="<?php echo DIR; ?>assets/css/structure.css">
<!--[if lt IE 9]>
<script src="assets/js/html5shiv.min.js"></script>
<script src="assets/js/respond.min.js"></script>
<![endif]-->
 <script src="<?php echo DIR; ?>assets/js/jquery-1.11.1.min.js"></script>
 <?php include("head_include.php"); ?>
</head>
<body>
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.10";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>


<div id="preloader">
  <div id="status"> </div>
</div>
<a class="scrollToTop" href="#"><i class="fa fa-angle-up"></i></a>

<?php include("header.php"); ?>

<section id="contentbody">
<div class="container">



        <?php
        
    $sqlQuery     = $db->query("SELECT * FROM blog_posts WHERE catID = '".$catid."' ORDER BY postID DESC");
    $count      = $sqlQuery->rowCount();
    
    $pageName = $url; //catName
    
    $adjacents = 1;
    global $records_per_page;
    
    $page = (int) (isset($_GET['page']) ? $_GET['page'] : 1);
    $page = ($page == 0 ? 1 : $page);
    $start = ($page-1) * $records_per_page;
    
    $next = $page + 1;    
    $prev = $page - 1;
    $last_page = ceil($count/$records_per_page);
    $second_last = $last_page - 1;

    
    $pagination = "";
    if($last_page > 1){
        $pagination .= "<div class='pagination'>";
        if($page > 1)
            echo $pagination.= "<a href='$pageName/page=1'>« First</a>";
        else
            $pagination.= "<span class='disabled'>« First</span>";
        
        if ($page > 1)
            $pagination.= "<a href='$pageName/page=".$prev."'>« Previous  </a>";
        else
            $pagination.= "<span class='disabled'>« Previous  </span>";   
        
        if ($last_page < 7 + ($adjacents * 2))
        {   
            for ($counter = 1; $counter <= $last_page; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     
                         
            }
        }
        elseif($last_page > 5 + ($adjacents * 2))
        {
            if($page < 1 + ($adjacents * 2))
            {
                for($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     
                }
                $pagination.= "...";
                $pagination.= "<a href='$pageName/page='".$second_last."'>".$second_last."</a>";
                $pagination.= "<a href='$pageName/page=".$last_page."'>".$last_page."</a>";   
           
           }
           elseif($last_page - ($adjacents * 2) > $page && $page > ($adjacents * 2))
           {
               $pagination.= "<a href='$pageName/page=1'>1</a>";
               $pagination.= "<a href='$pageName/page=2'>2</a>";
               $pagination.= "...";
               for($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
               {
                   if($counter == $page)
                       $pagination.= "<span class='current'>$counter</span>";
                   else
                       $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     
               }
               $pagination.= "..";
               $pagination.= "<a href='$pageName/page=".$second_last."'>".$second_last."</a>";
               $pagination.= "<a href='$pageName/page=".$last_page."'>".$last_page."</a>";   
           }
           else
           {
               $pagination.= "<a href='$pageName/page=1'>1</a>";
               $pagination.= "<a href='$pageName/page=2'>2</a>";
               $pagination.= "..";
               for($counter = $last_page - (2 + ($adjacents * 2)); $counter <= $last_page; $counter++)
               {
                   if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                   else
                        $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     
               }
           }
        }
        if($page < $counter - 1)
            $pagination.= "<a href='$pageName/page=".$next."'>Next »</a>";
        else
            $pagination.= "<span class='disabled'>Next »</span>";
        
        if($page < $last_page)
            $pagination.= "<a href='$pageName/page=".$last_page."'>Last »</a>";
        else
            $pagination.= "<span class='disabled'>Last »</span>";
        
        $pagination.= "</div>";       
    }
    $records     = $db->query("SELECT * FROM blog_posts WHERE catID = '".$catid."' ORDER BY postID DESC LIMIT $start, $records_per_page");
    $count      = $records->rowCount();
    
    if($count == ''){
    header('Location: index.php?page=1');
    exit;
    }
        
            try {
             echo '<div class="row">';
         echo '<div class=" col-sm-12 col-md-6 col-lg-6">';
         echo '<div class="row">';
         echo ' <div class="leftbar_content">';
         echo ' <h2>'.strtoupper($url).'</h2>';

                //$stm = $db->query('SELECT * FROM blog_posts WHERE catID = "'.$catid.'" ORDER BY postID DESC');
                
                //$stm->execute();
                while($rw = $records->fetch()){
            
            $img = $rw['postImg'];
            if(!empty($img)){
            $img ="<img src='".DIR."uploads/images/".$img."' alt=''";
            }else{
            $img = "";
            }
            $youtube = $rw['postYoutube'];
            if(!empty($youtube)){
            $youtube ='<div class="videoholder"> <iframe width="720" height="450" src="//www.youtube.com/embed/'.$youtube.'?autohide=1&fs=1&autoplay=0&iv_load_policy=3&rel=0&modestbranding=1&showinfo=0&controls=1&hd=1" frameborder="0" allowfullscreen=""></iframe></div>';
            }else{
            $youtube = "";
}                    
                
                 echo ' <div class="single_stuff wow fadeInDown">';
              echo '<div class="single_stuff_img"> <a href="'.DIR.'post/'.$rw['postSlug'].'">'.$img.$youtube.'</a> </div>';
              echo '<div class="single_stuff_article">';
                echo '<div class="single_sarticle_inner"> <a class="stuff_category" href="#">Technology</a>';
                  echo '<div class="stuff_article_inner"> <span class="stuff_date">'.date('jS M Y', strtotime($row['postDate'])).'</span>';
                    echo '<h2><a href="'.DIR.'post/'.$rw['postSlug'].'">'.$rw['postTitle'].'</a></h2>';
                   echo ' <p>'.$rw['postDesc'].'</p>';
                 echo '</div>';
                echo '</div>';
              echo '</div>';
            echo '</div>';
                }

            } catch(PDOException $e) {
                echo $e->getMessage();
            }
        ?>

            <div class="stuffpost_paginatinonarea wow slideInLeft">
              <ul class="newstuff_pagnav">
                <li><?php echo $pagination; ?></li>
              </ul>
            </div>
          </div>
        </div>
      </div>

<?php include("sidebar.php"); ?>     
<?php include("footer.php"); ?>

<script src="<?php echo DIR; ?>assets/js/jquery.min.js"></script>
<script src="<?php echo DIR; ?>assets/js/bootstrap.min.js"></script>
<script src="<?php echo DIR; ?>assets/js/wow.min.js"></script>
<script src="<?php echo DIR; ?>assets/js/custom.js"></script>
</body>
</html>

 

and here is my htaccess

Options +FollowSymLinks

RewriteEngine On


RewriteBase /blogg/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^post/(.*)\?*$ post.php?$1 [L,QSA]

RewriteRule ^category/([a-zA-Z0-9-/]+)/page/(.+)/?$ categories.php?$1&page=$2 [L,QSA]

RewriteRule ^category/(.*)\?*$ categories.php?$1 [L]

RewriteRule ^tag/(.*)\?*$ tag.php?$1 [L,QSA]

the category like localhost/blog/category/eateries i am able to open but when i tried to access page like localhost/blog/category/eateries/page/2 but i get redirected to index.php instead

Link to comment
Share on other sites

Too much code to look through.

RewriteRule ^post/(.*)\?*$ post.php?$1 [L,QSA]

RewriteRule ^category/([a-zA-Z0-9-/]+)/page/(.+)/?$ categories.php?$1&page=$2 [L,QSA]

RewriteRule ^category/(.*)\?*$ categories.php?$1 [L]

RewriteRule ^tag/(.*)\?*$ tag.php?$1 [L,QSA]
You're making this unnecessarily hard on yourself. Use query string parameters normally. Avoid using .* too. And the query string isn't part of the URL string being matched against. And either have trailing slashes or not - pick one, don't support both.

RewriteRule ^post/([a-zA-Z0-9-]+)$ post.php?title=$1 [L,QSA]

RewriteRule ^category/([a-zA-Z0-9-]+)/page/(\d+)$ categories.php?name=$1&page=$2 [L,QSA]

RewriteRule ^category/([a-zA-Z0-9-]+)$ categories.php?name=$1 [L]

RewriteRule ^tag/([a-zA-Z0-9-]+)$ tag.php?name=$1 [L,QSA]
Adjust your code accordingly and I think your problem will go away.
Link to comment
Share on other sites

so my way of pagination modification these codes are ok?

 

$sqlQuery     = $db->query("SELECT * FROM blog_posts WHERE catID = '".$catid."' ORDER BY postID DESC");

    $count      = $sqlQuery->rowCount();

    

    $pageName = $url; //catName

    

    $adjacents = 1;

    global $records_per_page;

    

    $page = (int) (isset($_GET['page']) ? $_GET['page'] : 1);

    $page = ($page == 0 ? 1 : $page);

    $start = ($page-1) * $records_per_page;

    

    $next = $page + 1;    

    $prev = $page - 1;

    $last_page = ceil($count/$records_per_page);

    $second_last = $last_page - 1;


    

    $pagination = "";

    if($last_page > 1){

        $pagination .= "<div class='pagination'>";

        if($page > 1)

            echo $pagination.= "<a href='$pageName/page=1'>« First</a>";

        else

            $pagination.= "<span class='disabled'>« First</span>";

        

        if ($page > 1)

            $pagination.= "<a href='$pageName/page=".$prev."'>« Previous  </a>";

        else

            $pagination.= "<span class='disabled'>« Previous  </span>";   

        

        if ($last_page < 7 + ($adjacents * 2))

        {   

            for ($counter = 1; $counter <= $last_page; $counter++)

            {

                if ($counter == $page)

                    $pagination.= "<span class='current'>$counter</span>";

                else

                    $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     

                         

            }

        }

        elseif($last_page > 5 + ($adjacents * 2))

        {

            if($page < 1 + ($adjacents * 2))

            {

                for($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)

                {

                    if($counter == $page)

                        $pagination.= "<span class='current'>$counter</span>";

                    else

                        $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     

                }

                $pagination.= "...";

                $pagination.= "<a href='$pageName/page='".$second_last."'>".$second_last."</a>";

                $pagination.= "<a href='$pageName/page=".$last_page."'>".$last_page."</a>";   

           

           }

           elseif($last_page - ($adjacents * 2) > $page && $page > ($adjacents * 2))

           {

               $pagination.= "<a href='$pageName/page=1'>1</a>";

               $pagination.= "<a href='$pageName/page=2'>2</a>";

               $pagination.= "...";

               for($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)

               {

                   if($counter == $page)

                       $pagination.= "<span class='current'>$counter</span>";

                   else

                       $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     

               }

               $pagination.= "..";

               $pagination.= "<a href='$pageName/page=".$second_last."'>".$second_last."</a>";

               $pagination.= "<a href='$pageName/page=".$last_page."'>".$last_page."</a>";   

           }

           else

           {

               $pagination.= "<a href='$pageName/page=1'>1</a>";

               $pagination.= "<a href='$pageName/page=2'>2</a>";

               $pagination.= "..";

               for($counter = $last_page - (2 + ($adjacents * 2)); $counter <= $last_page; $counter++)

               {

                   if($counter == $page)

                        $pagination.= "<span class='current'>$counter</span>";

                   else

                        $pagination.= "<a href='$pageName/page=".$counter."'>".$counter."</a>";     

               }

           }

        }

        if($page < $counter - 1)

            $pagination.= "<a href='$pageName/page=".$next."'>Next »</a>";

        else

            $pagination.= "<span class='disabled'>Next »</span>";

        

        if($page < $last_page)

            $pagination.= "<a href='$pageName/page=".$last_page."'>Last »</a>";

        else

            $pagination.= "<span class='disabled'>Last »</span>";

        

        $pagination.= "</div>";       

    }

Link to comment
Share on other sites

this is how it looks now but still not working :)

$pagination = "";
    if($last_page > 1){
        $pagination .= "<div class='pagination'>";
        if($page > 1)
            $pagination.= "<a href='$pageName/page/1'>« First</a>";
        else
            $pagination.= "<span class='disabled'>« First</span>";
        
        if ($page > 1)
            $pagination.= "<a href='$pageName/page/".$prev."'>« Previous  </a>";
        else
            $pagination.= "<span class='disabled'>« Previous  </span>";   
        
        if ($last_page < 7 + ($adjacents * 2))
        {   
            for ($counter = 1; $counter <= $last_page; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href='$pageName/page/".$counter."'>".$counter."</a>";     
                         
            }
        }
        elseif($last_page > 5 + ($adjacents * 2))
        {
            if($page < 1 + ($adjacents * 2))
            {
                for($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                    else
                        $pagination.= "<a href='$pageName/page/".$counter."'>".$counter."</a>";     
                }
                $pagination.= "...";
                $pagination.= "<a href='$pageName/page/'".$second_last."'>".$second_last."</a>";
                $pagination.= "<a href='$pageName/page/".$last_page."'>".$last_page."</a>";   
           
           }
           elseif($last_page - ($adjacents * 2) > $page && $page > ($adjacents * 2))
           {
               $pagination.= "<a href='$pageName/page/1'>1</a>";
               $pagination.= "<a href='$pageName/page/2'>2</a>";
               $pagination.= "...";
               for($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
               {
                   if($counter == $page)
                       $pagination.= "<span class='current'>$counter</span>";
                   else
                       $pagination.= "<a href='$pageName/page/".$counter."'>".$counter."</a>";     
               }
               $pagination.= "..";
               $pagination.= "<a href='$pageName/page/".$second_last."'>".$second_last."</a>";
               $pagination.= "<a href='$pageName/page/".$last_page."'>".$last_page."</a>";   
           }
           else
           {
               $pagination.= "<a href='$pageName/page/1'>1</a>";
               $pagination.= "<a href='$pageName/page/2'>2</a>";
               $pagination.= "..";
               for($counter = $last_page - (2 + ($adjacents * 2)); $counter <= $last_page; $counter++)
               {
                   if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                   else
                        $pagination.= "<a href='$pageName/page/".$counter."'>".$counter."</a>";     
               }
           }
        }
        if($page < $counter - 1)
            $pagination.= "<a href='$pageName/page/".$next."'>Next »</a>";
        else
            $pagination.= "<span class='disabled'>Next »</span>";
        
        if($page < $last_page)
            $pagination.= "<a href='$pageName/page/".$last_page."'>Last »</a>";
        else
            $pagination.= "<span class='disabled'>Last »</span>";
        
        $pagination.= "</div>";       
    }

Link to comment
Share on other sites

What are the href URLs you're creating? Are they using absolute paths? They need to be.

Because if you just have relative paths

and you put that on a page /blogg/category/X/page/M then the link is going to try to go to /blogg/category/X/page/M/category/X/page/N. Something which would be very easy to spot if you looked at the browser's address bar.

 

If it's not that then you'll have to say what "not working" means :psychic:

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.