1

How would I do so that the page I'm on won't be clickable?

$page = (isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1);
$limit = ($page - 1) * 15;
$sql = mysql_query("SELECT * FROM log LIMIT $limit, 15");
$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM log"),0);
$totalpages = ceil($totalres / 15);

for ($i = 1; $i <= $totalpages; $i++) {
  $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
}

<p><?php echo $pagination ?></p>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    http://thedailywtf.com/Articles/Pagination-Consternation.aspx - In all seriousness though, Pear::Pager might be worth looking into. – Thorarin Jul 26 '09 at 12:02

4 Answers4

3
if ($i == $page)
  $pagination .= "$i ";
else
  $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
Kawa
  • 1,478
  • 1
  • 15
  • 20
  • 1
    It would be better to use the `$page` variable defined earlier, instead of possibly unset or invalid `$_GET['page']` – Thorarin Jul 26 '09 at 12:12
1

With an if inside the for,

if ( $i != $page) {
    //Your code.
} 
else 
{ 
    //Same page.
    $pagination .= "&nbsp;" . $page . "&nbsp;"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dimitrios Mistriotis
  • 2,626
  • 3
  • 28
  • 45
1

Just need to modify the for loop to check what the current page is.

for ($i = 1; $i <= $totalpages; $i++) 
{
    if ($i != $page)
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    else
        $pagination .= " " . $i . " ";
}
RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
0

@RedBlueThing Has soon has I saw your post I got it to work, although I got the idea from a different stackoverflow question with a little modifying of the code.

<?php
echo "<section>";

$link = mysqli_connect("localhost", "root", "root", "user_db");

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


$offset = 4;
$x = 5;

$limit = ($page - 1) * $offset; 

$sql = mysqli_query($link,"SELECT * FROM cfp_blogs;");
$totalres = mysqli_num_rows($sql);
$totalpages = ceil($totalres / $offset);

    $stmt = mysqli_query($link, "SELECT * FROM blogs LIMIT $limit, $offset;");
        if($stmt->num_rows > 0){
            while($obj = mysqli_fetch_object($stmt)) { 
                echo "<h1>$obj->title</h1>";
                echo "<p>$obj->content</p>";
                $stmt2 = $link->query("SELECT COUNT(id) AS Tot FROM blogs");
                if($stmt2->num_rows > 0){

                while ($obj2 = $stmt2->fetch_object()){
                        echo "<p>($obj2->Tot)</p>";
                    }
                }
                else
                echo "<p>(0)</p>";
                $stmt2->close();

            }
        }
        else {
            echo "<section class='error'><h1>There was a problem with the database.</h1></section>";
        }

    mysqli_free_result($stmt);

for ($i = $page - $x; $i < $page; $i++)
{
    if ($i >= 1) { 
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    }
}

$pagination .= " " . $i . " ";

for ($i = $page + 1; $i <= $page + $x; $i++)
{
    if ($i <= $totalpages) {
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    }

}

/*for ($i = 1; $i <= $totalpages; $i++) 
{
    if ($i != $page){
        $pagination .= "<a href=\"$_SERVER[PHP_SELF]?page=$i\">$i</a> ";
    }
    else{
        $pagination .= " " . $i . " ";
    }
}*/

mysqli_close($link);

echo $pagination;

echo "</section>";
?>

The only thing I couldn't get was having a "next" or "previous" button. If anyone is up to the task of adding on those buttons, give it a shot. Other wise..... Enjoy..... :) This is my way of giving back to the stackoverflow community for helping me.

Community
  • 1
  • 1
blackbull77
  • 281
  • 1
  • 5
  • 14