tags:

views:

86

answers:

3

How would I do to bold the page I'm on and make it unclickable? In the link at the bottom? If I'm on page 3 for example: 1 2 3 4

This script:

// how many rows to show per page 
$max = 15;

$page = $_GET['page'];

// if $page is empty, set page number to 1
if (empty($page)) $page = 1;

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

$sql = mysql_query("SELECT * FROM threads LIMIT $limit, $max");

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

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

while ($thread = mysql_fetch_assoc($sql)) {
 echo $thread['title'];
}

for ($i = 1; $i <= $totalpages; $i++) { 

 echo '<a href='test.php?page=$i'>$i</a>';

}

Thanks in advance

+1  A: 

Something like this for your last loop:

for ($i = 1; $i <= $totalpages; $i++) { 

  if ($i == $page) {
    echo '<b>$i</b>';
  } else {
    echo '<a href='test.php?page=$i'>$i</a>';
  }

}
txwikinger
+3  A: 

The simple way would be to do something to the effect of:

for ($i = 1; $i <= $totalpages; $i++) {
 if($i == $page) {
  echo '<b>$i</b>'
 } else {
  echo '<a href='test.php?page=$i'>$i</a>';
 }
}
JonathonW
Well. same idea ;)
txwikinger
+1  A: 

No real difference on the previous two implementations (other than using a ternary operator), but you might want to add some error checking in there incase your database cannot return pages as it is offline, or a user maliciously provides anything other than a number to your $page variable. Here, I set the page number to zero if the page is blank or not a number:

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

And that final loop could be written as:

for ($i = 1; $i <= $totalpages; $i++) {
  echo ($i == $page) ? "<b>" . $i . "</b>" : "<a href='test.php?page=" . $i . ">" . $i . "</a>";
}
Gav