===== Paged Comments ===== >>Working for 1.1.6.0 to 1.1.6.4 (latest) Slight changes in the code in the vicinity of this modification, nothing serious.>>{{lastedit show="3"}} When the number of comments at the bottom of a page is too large (there is virtually no limit to their number), the comments block becomes user-unfriendly: ~-the page can take too long to load (especially for users with 56k dial-up connections) ~- navigation between comments becomes difficult. I've written a configurable **comments pager** to address this issue. The pager allows to switch between a "paged" and a "showall" view. The default number of comments per page is set by a **$limit_comments** session parameter, that can be changed in UserSettings. Paged comments come in two "flavours". ---- ==Screenshots:== **Flavour A) - previous/next links** http://wikka.jsnx.com/images/pagedcomments.jpg **Flavour B) - pagelist** http://wikka.jsnx.com/images/pagedcomments2.jpg ---- Here's the code. ==1. Create function ##""LoadCommentPage()""## in ##wikka.php##== %%(php) function LoadCommentPage($tag, $start, $limit) { return $this->LoadAll("SELECT * FROM ".$this->config["table_prefix"]."comments WHERE page_tag = '".mysql_real_escape_string($tag)."' ORDER BY time LIMIT ".$start.",".$limit); } %% ==2. Modify the comments block in ##handlers/page/show.php##== %%(php) if ($this->GetConfigValue("hide_comments") != 1) { // set default number of comments per page $d = ($_SESSION["limit_comments"])? $_SESSION["limit_comments"] : "10"; // set default visualization to "paged" $showall = ($_REQUEST["showall"])? $_REQUEST["showall"] : "0"; // get total number of comments for current page $n = count($this->LoadComments($this->tag)); // calculate starting item of last page $lastcomments = ceil($n/$d)*$d; $start = ($n > $d)? ($lastcomments-$d) : "0"; // set starting item and limit $s = (isset($_REQUEST["s"]))? $_REQUEST["s"] : $start; $l = (isset($_REQUEST["l"]))? $_REQUEST["l"] : $d; // set no limits for showall visualization if ($showall == 1) { $s = "0"; $l = "999"; } // Build pager links: /* // FLAVOUR A) - next/previous links // show "previous" link if needed if($s > 0) { $prev = " Href("","","l=".$l."&s=".($s-$l))."#comments\">« "; } // show "next" link if needed if($n > ($s + $l)) { $next = " Href("","","l=".$l."&s=".($s+$l))."#comments\">» "; } // create pager links $pager = ($prev || $next)? $prev.($s+1)."-".($s+$l).$next : ""; */ // FLAVOUR B) - pagelist if ($showall == 0 && $n > $l) { $i = 0; while ($i < $n) { if ($i == $s) { $pager .= ($i+1)."-".($i+$l)." | \n"; } else { $pager .= "Href("","","l=".$l."&s=".($i))."#comments\">".($i+1)."-".($i + $l)." | \n"; } $i = $i + $l; } } // create switcher link switch($showall) { case "0": // FLAVOUR A) // $switcher = ($n > $l)? " [Href("","","showall=1")."#comments\">Show all] " : ""; // FLAVOUR B) $switcher = ($n > $l)? " Href("","","showall=1")."#comments\">all " : ""; break; case "1": // FLAVOUR A) // $switcher = ($n > $d)? " [Href("","","showall=0")."#comments\">Show paged] " : ""; // FLAVOUR B) $switcher = ($n > $d)? " Href("","","showall=0")."#comments\">show paged " : ""; } // load comments for this page with limits $comments = $this->LoadCommentPage($this->tag, $s, $l); // store comments display in session $tag = $this->GetPageTag(); if (!isset($_SESSION["show_comments"][$tag])) $_SESSION["show_comments"][$tag] = ($this->UserWantsComments() ? "1" : "0"); if (isset($_REQUEST["show_comments"])){ switch($_REQUEST["show_comments"]) { case "0": $_SESSION["show_comments"][$tag] = 0; break; case "1": $_SESSION["show_comments"][$tag] = 1; break; } } // display comments! if ($_SESSION["show_comments"][$tag]) { // display comments header ?>
 Comments [">Hide comments/form]
GetUserName(); foreach ($comments as $comment) { print("
\n"); print("".$comment["comment"]."\n"); print("\t
\n-- ".$this->Format($comment["user"])." (".$comment["time"].")\n"); $current_user = $this->GetUserName(); if ($this->UserIsOwner() || $current_user == $comment["user"] || ($this->config['anony_delete_own_comments'] && $current_user == $comment["user"]) ) { ?> FormOpen("delcomment"); ?> " /> FormClose(); ?> \n"); print("
\n"); } // Show bottom pager link if ($showall == 0 && $n > $l) { print "
".$pager.$switcher."
\n"; } } // display comment form print("
\n"); if ($this->HasAccess("comment")) {?> FormOpen("addcomment"); ?> FormClose(); ?> \n"); } else { ?>
There are no comments on this page. "); $showcomments_text = "Add comment"; break; case 1: print("

There is one comment on this page. "); $showcomments_text = "Display comment"; break; default: print("

There are ".$n." comments on this page. "); $showcomments_text = "Display comments"; } ?> [$showcomments_text"; ?>]

SetUser($this->LoadUser($user["name"])); // forward $this->SetMessage("User settings stored!"); $this->Redirect($this->href()); %% **modified** %%(php) $this->SetUser($this->LoadUser($user["name"])); $_SESSION["limit_comments"] = ($_REQUEST["limit_comments"])? $_REQUEST["limit_comments"] : "10"; // forward $this->SetMessage("User settings stored!"); $this->Redirect($this->href()); %% ==B)== **original** %%(php) Page revisions list limit: " size="40" /> '" /> %% **modified** %%(php) Page revisions list limit: " size="40" /> Comments per page: '" /> %% ---- CategoryDevelopmentHandlers