Jump to content

Display Most Viewed Knowledge Base Articles on Home Page?


imaginit

Recommended Posts

I want to display the most viewed knowledge base articles, and in the future maybe other items from the internal sections of the site on custom pages. This does not seem to be easily possible. The variable $kbmostviews is not available on any page but knowledgebase.php.

 

I am not sure if I can use some built in function to get that variable on an additional page or if I have to build a custom smarty plugin that connects to the DB and such. Any insight would be appreciated. Thanks!

Link to comment
Share on other sites

I figured this out and wrote a really simple function to populate the proper variable and assign it to a smarty variable. This function will only work within the WHMCS system. Meaning you have to create a page as outlined at the below link and then use thise code in that. I did this to create my own custom home page that can display knowledge base article information on it.

 

http://wiki.whmcs.com/Creating_Pages

 

function list_kb_most_viewed($numarticles)
{
   $kbmostviews = mysql_query("SELECT *, replace(replace(replace(replace(replace(replace(title,' ', '_'),'&','and'),'&','and'),';', ''),'/', 'or'),'\\\\', '') as urlfriendlytitle FROM tblknowledgebase ORDER BY views DESC LIMIT 0,$numarticles");
   while ($row = mysql_fetch_assoc($kbmostviews)) $data[] = $row;
   return $data;
}

Link to comment
Share on other sites

  • 2 weeks later...

To show most viewed knowledgebase articles to an external page you should use this part of code.

 

Thanks to netzone :lol:

 

<?php
// Connects to your Database 
mysql_connect("host", "user", "pass") or die(mysql_error()); // host: your host or IP, user: database user, password: your database user's password
mysql_select_db("database") or die(mysql_error());// database of whmcs

$x=1;
// truncate the text to only 80 chars, add ... to the end
function truncat($text) {
$text = substr($text,0,80);
$text = substr($text,0,strrpos($text,' '));
$text = $text." ... ";
return $text;
}
// format the date to mm/dd/yyyy
function formatdat($date) {
$year = substr ($date, 0, 4);
$month = substr ($date, 5, 2);
$day = substr ($date, 8, 2);
return $day.'/'.$month.'/'.$year;
}
// limit the number of announcements to return. Default is 15
$limit = 15;

$result = mysql_query("SELECT * FROM tblknowledgebase ORDER BY views DESC LIMIT ".$limit);
while($data = mysql_fetch_array ($result))
{
$id = $data['id'];
$date = $data['date'];
$title = truncat($data['title']);
$date = formatdat($date);
echo'<div><a href="http://www.yourdomain.com/knowledgebase.php?action=displayarticle&id='.$id.'" class="tekst"><b>'.$title.'</b></a><br>
';
echo'</div>';

}

?> 

Link to comment
Share on other sites

  • 1 year later...

I have a question, How about when you search something in the Knowledgebase.php, it gives you something like this on the url :

 

knowledgebase.php?action=search

 

Does this mean that the $kbmostviews is available on that page?, because I've been trying to use it and it doesn't appear to work.

 

Maybe I'm doing something wrong but I'd like to know if the variable is available or not.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated