Hi everyone,

i have a very basic pagination code for my site however i would like to adapt the links as they are displayed, so far the script just outputs numbered links but there is no such styling to the link. i would like the active link to be in bold and not clickable. i.e if im on page 4 the page four is in bold with no href! here is my code:

<?php 
include ('db-conn.php');

$max = 1; //amount of articles per page. change to what to want
if (isset($_GET['p'])){
$p = $_GET['p'];}

if(empty($p))

{

$p = 1;

}

$limits = ($p - 1) * $max; 

//view the news article!

if(isset($_GET['act']) && $_GET['act'] == "view")

{

$id = $_GET['id'];

$sql = mysql_query("SELECT * FROM news WHERE id = '$id'");

while($r = mysql_fetch_array($sql))

{

$title = $r['title'];

$story = $r['story'];

$author = $r['author'];
$story = stripslashes($story); 
echo "<div><p>$title</p><p>$author</p><p>$story</p></div>";
echo "<a href=\"".$_SERVER['HTTP_REFERER']."\">Back</a>";
}
}else{
//view all the news articles in rows

$sql = mysql_query("SELECT * FROM news LIMIT ".$limits.",$max") or die(mysql_error());

//the total rows in the table

$totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM news"),0);	

//the total number of pages (calculated result), math stuff...

$totalpages = ceil($totalres / $max); 

//the table


while($r = mysql_fetch_array($sql))

{

$id = $r['id'];

$title = $r['title'];

$author = $r['author'];

echo "<h3><a href='news.php?act=view&id=$id'>$title</a></h3><div>$author</div>";

}

echo "Pages to View <br />";
echo "<div id=\"pagination\"";
for($i = 1; $i <= $totalpages; $i++){ 

//this is the pagination link

echo "<a class=\"pglink\" href='news.php?p=$i'>$i</a>&nbsp;&nbsp";

}
echo "</div>";
}



?>

Recommended Answers

All 3 Replies

for($i = 1; $i <= $totalpages; $i++)
{
    if($i == $p)
    {
    	 echo "<b>$i</b>&nbsp;&nbsp";
    }
    else
    {
        echo "<a class=\"pglink\" href='news.php?p=$i'>$i</a>&nbsp;&nbsp";
    }
}
for($i = 1; $i <= $totalpages; $i++)
{
    if($i == $p)
    {
    	 echo "<b>$i</b>&nbsp;&nbsp";
    }
    else
    {
        echo "<a class=\"pglink\" href='news.php?p=$i'>$i</a>&nbsp;&nbsp";
    }
}

Many thanks for the code, the theory behind the code works perfectly however, i have just tested it and i have this problem.
the first page works great the '1' is in bold and unclickable an then i clicked in the '2' but the one '1' and '2' became bold and unclickable!? any ideas?

Cheers...

######################
Scrap all that, it worked great, thanks saiprem, i forgot to close a previous div tag!! oops
Cheers...

Write the variable outside from the string. Example:

echo "<a class=\"pglink\" href=\"news.php?p=" . $i . "\">" . $i . "</a>&nbsp;&nbsp";

This may be help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.