cdeese8 Posted February 14, 2019 Share Posted February 14, 2019 How do I load data on the knowledgebasecat.tpl page? When I add {debug} to the file I can't see the specific "votes" and "useful" data being loaded. But if I add {debug} to the knowledgebasearticle.tpl I can see them being loaded. How do I get that specific smarty data / variable to pass on the cat page? {$kbarticle.votes} {$kbarticle.useful} I noticed the knowledgebasecat.tpl loads the "views" .. I just wish it also loaded the votes and useful smarty data too. Any ideas? Anything you can offer would be most helpful. Thanks, 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted February 14, 2019 Share Posted February 14, 2019 Submit a feature request and while you're waiting use a hook and template changes to do what you need. <?php use WHMCS\Database\Capsule; add_hook('ClientAreaPageKnowledgebase', 1, function($vars) { $x=0; foreach($vars['kbarticles'] as $article) { $dbarticle = Capsule::table('tblknowledgebase')->where('id', '=', $article['id'])->first(); // get article from database since we aren't given votes / useful here. $vars['kbarticles'][$x]['useful'] = $dbarticle->useful; // add to the array $x++; } return $vars; }); Then in the template just add: {kbarticle.useful} where you want within the article loop. 1 Quote Link to comment Share on other sites More sharing options...
cdeese8 Posted February 15, 2019 Author Share Posted February 15, 2019 (edited) Thank you very much @steven99 - at first, i tried to create two hook files (and dupe the code and replace the keyword) but that didn't work, so i just duped what you shared, merged it into a single hook file and it magically works. Brilliant! If you have any advise or wisdom, maybe this isn't written properly... I'm all ears. Thanks again!! Now, on the knowledgebasecat.tpl page, I can use both {$kbarticle.votes} and {$kbarticle.useful} to get the extracted data I desired. <?php // written by steven99 // https://whmcs.community/topic/293372-how-do-i-make-smarty-data-load-on-kbcat-page-it-loads-on-kbarticle-page-just-fine/?do=findComment&comment=1311909 use WHMCS\Database\Capsule; add_hook('ClientAreaPageKnowledgebase', 1, function($vars) { $x=0; foreach($vars['kbarticles'] as $article) { $dbarticle = Capsule::table('tblknowledgebase')->where('id', '=', $article['id'])->first(); // get article from database since we aren't given useful here. $vars['kbarticles'][$x]['useful'] = $dbarticle->useful; // add to the array $x++; } $x=0; foreach($vars['kbarticles'] as $article) { $dbarticle = Capsule::table('tblknowledgebase')->where('id', '=', $article['id'])->first(); // get article from database since we aren't given votes here. $vars['kbarticles'][$x]['votes'] = $dbarticle->votes; // add to the array $x++; } return $vars; }); Edited February 15, 2019 by cdeese8 updated code 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted February 15, 2019 Share Posted February 15, 2019 i'd suggest writing the hook as below - it's a little redundant to use two separate database queries and foreach loops, so i've merged them into just one... <?php # Add Votes & Useful Values To kbarticles Array Hook # Written by brian! use Illuminate\Database\Capsule\Manager as Capsule; function kbarticles_add_votes_and_useful_fields_hook($vars) { if ($vars['templatefile'] == 'knowledgebasecat') { $kbarticles = $vars['kbarticles']; foreach($kbarticles as $key => $article) { $results = Capsule::table('tblknowledgebase')->where('id',$article['id'])->select('votes','useful')->first(); $kbarticles[$key]['votes'] = $results->votes; $kbarticles[$key]['useful'] = $results->useful; } return array("kbarticles" => $kbarticles); } } add_hook("ClientAreaPageKnowledgebase", 1, "kbarticles_add_votes_and_useful_fields_hook"); ?> this will still give you the $kbarticle.votes} and {$kbarticle.useful} values that you can output in your template. 1 Quote Link to comment Share on other sites More sharing options...
cdeese8 Posted February 15, 2019 Author Share Posted February 15, 2019 Excellent optimization and thanks for the clean hook. 😋 You know, it was nice to see Steven drop in here but when I see Brian... it's ALWAYS a treat! Thanks dude. It works perfectly! If you are still reading this, and happen to know the answer, what is the difference in the hook between the two snippets? use Illuminate\Database\Capsule\Manager as Capsule; use WHMCS\Database\Capsule; Is one telling Smarty to dig into the database and do a global search and the other is specifically telling Smarty to do something else? Are the two hooks a combination of PHP, Smarty and SQL language? I've never seen things like "$x=0;" and "$x++;". Thanks again!! 😎 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted February 16, 2019 Share Posted February 16, 2019 19 hours ago, cdeese8 said: If you are still reading this I read everything (I think!)... 19 hours ago, cdeese8 said: what is the difference in the hook between the two snippets? use Illuminate\Database\Capsule\Manager as Capsule; use WHMCS\Database\Capsule; Is one telling Smarty to dig into the database and do a global search and the other is specifically telling Smarty to do something else? Are the two hooks a combination of PHP, Smarty and SQL language? they're just telling the hook to get ready, we're about to query/update the database... mine is probably from how the example hooks were posted when v6 came out; steven's is how they're posted now... they're both doing the same thing and at this stage, I wouldn't worry about it too much - when i'm writing a new hook, i'm using one of a number of hook templates and it will likely contain that line that i've always used... and I don't know if that new method would only work on v7.... so when I get the chance, i'll have a play and see, but don't worry about it for now - as long as the hook can find Capsule, then the queries will work. 19 hours ago, cdeese8 said: I've never seen things like "$x=0;" and "$x++;". they're fine and are just incremental counter, e.g., $x++ just adds 1 to the value of $x... steven99 was using a loop with a value but no key; whereas in mine, i'm specifying to use both the value and the key, and subsequently don't need to use a separate counter variable. 1 Quote Link to comment Share on other sites More sharing options...
steven99 Posted February 18, 2019 Share Posted February 18, 2019 Was in a bad coding mood I guess and I know better on the loops. 😉 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.