Search code examples
phphtmlcsssearchpagination

pagination in css/php


two questions:

--1-- it displays all the number of pages. but i'd like it to display as:

<< Prev . . 3 4 [5] 6 7 . . Next >>

--2-- when i hover on the current page no, it should change color or increase the font-size, but when i modify the css of a:hover, all the page nos change color or size instead of the one which i'm pointing to. also, when modifying a:active, nothing happens.

this is my paging code in php:

 $self = $_SERVER['PHP_SELF'];
            $limit = 2; //Number of results per page
            $numpages=ceil($totalrows/$limit);

            $query = $query." ORDER BY idQuotes LIMIT " . ($page-1)*$limit . ",$limit";
            $result = mysql_query($query, $conn)
             or die('Error:' .mysql_error());
?>

<div class="caption">Search Results</div>
<div class="center_div">
<table>
    <?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
        $cQuote =  highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
        ?>
        <tr>
        <td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td>
            <td style="font-size:16px;"><?php echo $cQuote; ?></td>
            <td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
            <td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
        </tr>
    <?php } ?>
</table>
</div>

<div class="searchmain">
<?php
   //Create and print the Navigation bar
       $nav="";
       $next = $page+1;
       $prev = $page-1;
       if($page > 1) {
            $nav .= "<span class=\"searchpage\"><a onclick=\"showPage('','$prev'); return false;\" href=\"$self?page=" . $prev . "&q=" .urlencode($search_result) . "\">< Prev</a></span>";

            $first = "<span class=\"searchpage\"><a onclick=\"showPage('','1'); return false;\" href=\"$self?page=1&q=" .urlencode($search_result) . "\"> << </a></span>" ;
        }

        else {
            $nav .= "&nbsp;";
            $first = "&nbsp;";
        }

       for($i = 1 ; $i <= $numpages ; $i++) {
            if($i == $page) {
                $nav .= "<span class=\"searchpage\">$i</span>";
            }else{
                $nav .= "<span class=\"searchpage\"><a onclick=\"showPage('',$i); return false;\" href=\"$self?page=" . $i . "&q=" .urlencode($search_result) . "\">$i</a></span>";
            }
        }

        if($page < $numpages) {
            $nav .= "<span class=\"searchpage\"><a onclick=\"showPage('','$next'); return false;\" href=\"$self?page=" . $next . "&q=" .urlencode($search_result) . "\">Next ></a></span>";

            $last = "<span class=\"searchpage\"><a onclick=\"showPage('','$numpages'); return false;\" href=\"$self?page=$numpages&q=" .urlencode($search_result) . "\"> >> </a></span>";
        }

        else {

             $nav .= "&nbsp;";
             $last = "&nbsp;";
        }


        echo  $first . $nav . $last;
?>

</div>

and this is how it displays currently:

alt text http://img714.imageshack.us/img714/7184/pag1l.jpg

css:

.searchmain {
    margin:30px;
    text-align: center;
}
.searchpage {
   border: solid 1px #ddd;
   background: #fff;
   text-align:left;
   font-size: 16px;
   padding:9px 12px;
   color: #FEBE33;
   margin-left:2px;
}

.searchpage a{
    text-decoration: none;
    color: #808080;
}

.searchpage a:hover {
    color: #FEBE33;
    border-color: #036;
    text-decoration: none;
}

.searchpage a:visited {
    color: #808080;
}

Solution

  • The solution to your first program seems pretty straightforward; you know the number of surrounding links you want to display so you can simply loop from current_page - surrounding_links to current_page + surrounding_links. Add some conditions for the beginning, the end and for when to show the dots and you're done.

    For the css; I´m not entirely sure what you want to achieve but I think you can solve it by using just a tags, the surrounding spans seem redundant, you only need them for the items that are not links (.no_link in the example below):

    a, .no_link {
        float: left;
        display: block;
        padding:9px 12px;
        border: solid 1px #ddd;
        background: #fff;
        text-align:left;
        font-size: 16px;
    }
    a {
        color: #808080;
    }
    a:hover {
        color: #FEBE33;
        border-color: #036;
    }
    .no_link {
        color: #FEBE33;
    }