[SOLVED] Adding the Top Ten to main page

In this board you can talk about general questions about phpMyFAQ

Moderator: Thorsten

Post Reply
openeye
Posts: 11
Joined: Thu Mar 28, 2013 6:39 pm

[SOLVED] Adding the Top Ten to main page

Post by openeye »

I know, I know, this again?

I've pored over the forum and tried the typical suggestions (this and this), but the most recent post I found is 8 years old, and it seems things have changed somewhat since then.

I understand the basic principles, but I'm struggling with the execution. So with that in mind, how can I use {writeTopTenRow} in the main.tpl file? What do I need to add/change in the main.php file to make this happen?
Last edited by openeye on Mon Apr 01, 2013 6:15 pm, edited 1 time in total.
openeye
Posts: 11
Joined: Thu Mar 28, 2013 6:39 pm

Re: Adding the Top Ten to main page

Post by openeye »

OK, I finally figured it out on my own. For those wondering, the change I made to main.php (and remember, I'm running version 2.7.9) was to add

Code: Select all

'writeTopTenRow'        => $topten,
to the processTemplate() method. So what you end up with is:

Code: Select all

$tpl->processTemplate('writeContent', array(
    'writeTopTenRow'        => $topten,
    'writeNewsHeader'       => $writeNewsHeader,
    'writeNewsRSS'          => $writeNewsRSS,
    'writeNews'             => $news->getNews($archived),
    'showAllNews'           => $showAllNews,
    'writeNumberOfArticles' => $plr->getMsg('plmsgHomeArticlesOnline', $faq->getNumberOfRecords($LANGCODE))));

$tpl->includeTemplate('writeContent', 'index');
Now hang on, because you're not done yet. That $topten variable is the result of some parsing I also added to the main.php file. The method you need to get the top ten already exists, but it returns an array, thus:

Code: Select all

$topten_arr = $faq->getTopTen(); // Get the top ten
$topten     = ''; // Instantiate the $topten var
if(array_key_exists('error', $topten_arr)) { // Check to see if zero results were returned
    $topten = 'No results!'; // Output in case the were zero results
} else {
    for($i=0; $i<count($topten_arr['id']); $i++) { // Loop for as many entries as exist (because it may be fewer than 10)
        $topten .= ''; // Build your output HTML
    }
}
Note that the $topten_arr['id'] is the result of a bit of custom fiddling I did with in the inc/Faq.php file.

If you want to see what $faq->getTopTen() outputs so you can wrap your head around the parsing of it, add

Code: Select all

echo '<pre>';
var_dump($output);
echo '</pre>';
die();
immediately before

Code: Select all

return $output;
to the getTopTen() method in inc/Faq.php.

UPDATE 2013-07-08: I just upgraded to 2.8.1 and had to re-setup all of this. I'm pleased to say that it still works.
Post Reply