Tag cloud with pagination

I have a tag cloud code which is working fine but I would really like to limit the amount of tags shown per page. If there are more tags than the limit allows I would like to have some pagination to view the remaining tags.

Here’s my cloudtag code;

<?php
function get_tag_data() { 
  $result = mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC"); 
  while($row = mysql_fetch_array($result)) { 
    $arr[$row['tag']] = $row['count'];
  } 
  ksort($arr); 
  return $arr; 
}

function get_tag_cloud() {

    // Default font sizes
    $min_font_size = 12;
    $max_font_size = 18;

    // Pull in tag data
    $tags = get_tag_data();

    $minimum_count = min(array_values($tags));
    $maximum_count = max(array_values($tags));
    $spread = $maximum_count - $minimum_count;

    if($spread == 0) {
        $spread = 1;
    }
	
    $cloud_html = '';
    $cloud_tags = array(); // create an array to hold tag code
    foreach ($tags as $tag => $count) {
	
	$replaceTag = array('_'=>'-', ' '=>'-');
	$ammendTag=strtr($tag, $replaceTag); 
	 
        $size = $min_font_size + ($count - $minimum_count) 
            * ($max_font_size - $min_font_size) / $spread;
        $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
            . '" class="tag_cloud" href="http://localhost/voucherhound_v2/discountsoffers/' . $ammendTag 
            . '">' 
            . htmlspecialchars(stripslashes($tag)) . '</a>';
    }
    $cloud_html = join("\
", $cloud_tags) . "\
";
    return $cloud_html;

}

?>

<style type="text/css">
.tag_cloud { padding: 3px ; text-decoration: none;}
.tag_cloud:link  { color: #000; }
.tag_cloud:visited { color: #000; }
.tag_cloud:hover { color: #ffffff; background: #6da9eb; }
.tag_cloud:active { color: #ffffff; background: #6da9eb; }
</style>

<div id="wrapper">
    <!-- BEGIN Tag Cloud --> 
    <?php
    // Display the Tag Cloud in an HTML format
    print get_tag_cloud();
    ?>
    <!-- END Tag Cloud -->
</div>

Does anyone have any suggestions on a way of including pagination into this code? I do have a pagination code but am not sure of how to incorporate it into this code.

Pagination code;

<?php
if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
} // if
$query = "SELECT count(*) FROM table WHERE ...";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 15;
$lastpage      = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if
if ($pageno < 1) {
   $pageno = 1;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM tags $limit";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
... process contents of $result ...

if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if
?>

Thanks

How about - Perfect PHP Pagination

Thanks for the suggestion. I shall take a look.

Off Topic:

What the?

This is three times in as many days that I’ve seen a variant of this code, weird.

Is this from a tutorial Richard?

Yes it is a tutorial. The link is here.

I have managed to get my code working. I still have a problem however. If there are 3 pages of results, the first page renders fine but the next page simply inserts the font size as the maximum. Can’t find out where the problem lies.

My ammended code

<?php

function get_tag_data() {

//pagination
if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
} // if

$query = "SELECT * FROM tags";
$result = mysql_query($query);
$numrows=mysql_num_rows($result);

$rows_per_page = 80;
$lastpage      = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if
if ($pageno < 1) {
   $pageno = 1;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
//end of pagination

  $result = mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC $limit"); 
  while($row = mysql_fetch_array($result)) { 
    $arr[$row['tag']] = $row['count'];
  } 
  ksort($arr);
  
  //echo $numrows.'-'.$pageno.'-'.$lastpage;
  
 //pagination
  if ($pageno == 1) {
	//echo'<tr><td>';
	//echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
	//echo "<tr><td>";
   	echo " << FIRST PAGE &nbsp; PREVIOUS&nbsp;";
} else {
   	echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'><<&nbsp;FIRST PAGE&nbsp;</a> ";
   $prevpage = $pageno-1;
   	echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>&nbsp;PREVIOUS&nbsp;</a>";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
   	echo " NEXT > LAST >><br><br>";
} else {
   $nextpage = $pageno+1;
   	echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>&nbsp;NEXT&nbsp;</a> ";
   	echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>&nbsp;LAST PAGE&nbsp;>></a><br><br>";
   	//echo'</tr></td>';
} // if
//end of pagination

  return $arr; 
}

function get_tag_cloud() {

    // Default font sizes
    $min_font_size = 12;
    $max_font_size = 20;

    // Pull in tag data
    $tags = get_tag_data();

    $minimum_count = min(array_values($tags));
    $maximum_count = max(array_values($tags));
    $spread = $maximum_count - $minimum_count;

    if($spread == 0) {
        $spread = 1;
    }
	
    $cloud_html = '';
    $cloud_tags = array(); // create an array to hold tag code
    foreach ($tags as $tag => $count) {
	
	//url ammendment
	$replaceTag = array('_'=>'-', ' '=>'-');
	$ammendTag=strtr($tag, $replaceTag); 
	//end of url ammendment
        $size = $min_font_size + ($count - $minimum_count) 
            * ($max_font_size - $min_font_size) / $spread;
        $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
            . '" class="tag_cloud" href="http://localhost/test_site/test/' . $ammendTag 
            . '">' 
            . htmlspecialchars(stripslashes($tag)) . '</a>';
    }
    $cloud_html = join("\
", $cloud_tags) . "\
";
    return $cloud_html;

}

?>

<style type="text/css">
.tag_cloud { padding: 3px ; text-decoration: none;}
.tag_cloud:link  { color: #000; }
.tag_cloud:visited { color: #000; }
.tag_cloud:hover { color: #ffffff; background: #6da9eb; }
.tag_cloud:active { color: #ffffff; background: #6da9eb; }
</style>

<div id="wrapper">
    <!-- BEGIN Tag Cloud --> 
    <?php
    // Display the Tag Cloud in an HTML format
    print get_tag_cloud();
    ?>
    <!-- END Tag Cloud -->
</div>