osmanboy Posted May 2, 2020 Share Posted May 2, 2020 Hi, I have an issue with an hook, i cant get it to fetch information from the database. I have searched everything but no matter what i try it doenst load. The Tabel is tbladdonmodules The module is ctaa The settings is has to fetch are: logo Dropdown Field Name text I tryed the following: $logo = Capsule::table('tbladdonmodules')->where('ctaa', 'logo')->value('logo'); But this wont return anything. Could someone help me? 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted May 2, 2020 Share Posted May 2, 2020 wouldn't it be... ? $logo = Capsule::table('tbladdonmodules')->where('module','ctaa')->where('setting','logo')->value('logo'); 1 Quote Link to comment Share on other sites More sharing options...
osmanboy Posted May 2, 2020 Author Share Posted May 2, 2020 22 minutes ago, brian! said: wouldn't it be... ? $logo = Capsule::table('tbladdonmodules')->where('module','ctaa')->where('setting','logo')->value('logo'); Hey, I tryed this but it still wont work: <? use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $template = $vars['template']; return <<<HTML <link href="{$WEB_ROOT}/modules/addons/ctaa/css/css.css" rel="stylesheet"> <a href="http://google.com"><br/>$logo</a> HTML; $logo = Capsule::table('tbladdonmodules')->where('module','ctaa')->where('setting','logo')->value('logo'); }); 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 2, 2020 Share Posted May 2, 2020 3 hours ago, osmanboy said: logo Dropdown Field Name text Are you trying to get 3 values (logo, dropdown field name and text) with a single query? If so, this query should work. $Data = Capsule::select(Capsule::raw('SELECT value FROM tbladdonmodules WHERE module = "ctaa" AND setting IN ("logo", "Dropdown Field Name", "text") LIMIT 3')); foreach ($Data as $v) { echo $v->value . '<br>'; } I'm using Capsule::raw because I dislike Laravel. It is unnecessarily verbose 🤮 1 Quote Link to comment Share on other sites More sharing options...
osmanboy Posted May 2, 2020 Author Share Posted May 2, 2020 3 minutes ago, Kian said: Are you trying to get 3 values (logo, dropdown field name and text) with a single query? If so, this query should work. $Data = Capsule::select(Capsule::raw('SELECT value FROM tbladdonmodules WHERE module = "ctaa" AND setting IN ("logo", "Dropdown Field Name", "text") LIMIT 3')); foreach ($Data as $v) { echo $v->value . '<br>'; } I'm using Capsule::raw because I dislike Laravel. It is unnecessarily verbose 🤮 Hi, i will have to split them up, so $logo, $dropdown and $text 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 2, 2020 Share Posted May 2, 2020 Then simply set key/value pairs like follows: <?php $Data = Capsule::select(Capsule::raw('SELECT value FROM tbladdonmodules WHERE module = "ctaa" AND setting IN ("logo", "Dropdown Field Name", "text") LIMIT 3')); foreach ($Data as $v) { $module->{strtolower(str_replace(' ', '-', $v->setting))} = $v->value; } // You can access values like follows: echo $module->logo; echo '<br>'; echo $module->dropdown-field-name; echo '<br>'; echo $module->text; I had to use str_replace and strtolower since one of your settings is "Dropdown Field Name". 1 Quote Link to comment Share on other sites More sharing options...
osmanboy Posted May 2, 2020 Author Share Posted May 2, 2020 18 minutes ago, Kian said: Then simply set key/value pairs like follows: <?php $Data = Capsule::select(Capsule::raw('SELECT value FROM tbladdonmodules WHERE module = "ctaa" AND setting IN ("logo", "Dropdown Field Name", "text") LIMIT 3')); foreach ($Data as $v) { $module->{strtolower(str_replace(' ', '-', $v->setting))} = $v->value; } // You can access values like follows: echo $module->logo; echo '<br>'; echo $module->dropdown-field-name; echo '<br>'; echo $module->text; I had to use str_replace and strtolower since one of your settings is "Dropdown Field Name". Hi, this only results in an 0 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted May 3, 2020 Share Posted May 3, 2020 16 hours ago, osmanboy said: I tried this but it still wont work: two problems - firstly, the query should really have been... $logo = Capsule::table('tbladdonmodules')->where('module','ctaa')->where('setting','logo')->value('value'); secondly, you should have defined what $logo was before trying to output it. <? use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $template = $vars['template']; $logo = Capsule::table('tbladdonmodules')->where('module','ctaa')->where('setting','logo')->value('value'); return <<<HTML <link href="{$WEB_ROOT}/modules/addons/ctaa/css/css.css" rel="stylesheet"> <a href="http://google.com"><br/>$logo</a> HTML; }); and if you wanted multiple values, another option would be pluck... <? use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $template = $vars['template']; $ctaa = Capsule::table('tbladdonmodules')->where('module','ctaa')->whereIn('setting',['logo','dropdown_field_name','text'])->pluck('value','setting'); $logo = $ctaa['logo']; $dropdown = $ctaa['dropdown_field_name']; $test = $ctaa['text']; return <<<HTML <link href="{$WEB_ROOT}/modules/addons/ctaa/css/css.css" rel="stylesheet"> <a href="http://google.com"><br/>$logo</a> HTML; }); that will give you 3 variables ($logo, $dropdown and $text) that you can use in the hook (tested as working with google analytics settings). 1 Quote Link to comment Share on other sites More sharing options...
osmanboy Posted May 3, 2020 Author Share Posted May 3, 2020 Just now, brian! said: two problems - firstly, the query should really have been... $logo = Capsule::table('tbladdonmodules')->where('module','ctaa')->where('setting','logo')->value('value'); secondly, you should have defined what $logo was before trying to output it. <? use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $template = $vars['template']; $logo = Capsule::table('tbladdonmodules')->where('module','ctaa')->where('setting','logo')->value('value'); return <<<HTML <link href="{$WEB_ROOT}/modules/addons/ctaa/css/css.css" rel="stylesheet"> <a href="http://google.com"><br/>$logo</a> HTML; }); and if you wanted multiple values, another option would be pluck... <? use WHMCS\Database\Capsule; add_hook('ClientAreaHeadOutput', 1, function($vars) { $template = $vars['template']; $ctaa = Capsule::table('tbladdonmodules')->where('module','ctaa')->whereIn('setting',['logo','dropdown_field_name','text'])->pluck('value','setting'); $logo = $ctaa['logo']; $dropdown = $ctaa['dropdown_field_name']; $test = $ctaa['text']; return <<<HTML <link href="{$WEB_ROOT}/modules/addons/ctaa/css/css.css" rel="stylesheet"> <a href="http://google.com"><br/>$logo</a> HTML; }); that will give you 3 variables ($logo, $dropdown and $text) that you can use in the hook (tested as working with google analytics settings). Thanks, I just fixed it like 5 minutes ago. 🙂 0 Quote Link to comment Share on other sites More sharing options...
bear Posted May 3, 2020 Share Posted May 3, 2020 2 hours ago, osmanboy said: I just fixed it like 5 minutes ago Perhaps sharing the fix would be helpful to those that may have the same issue? 0 Quote Link to comment Share on other sites More sharing options...
osmanboy Posted May 3, 2020 Author Share Posted May 3, 2020 1 minute ago, bear said: Perhaps sharing the fix would be helpful to those that may have the same issue? Sure, $Data = Capsule::select(Capsule::raw('SELECT value FROM tbladdonmodules WHERE module = "ctaa" AND setting IN ("logo") LIMIT 1')); foreach ($Data as $v) And for the next value i used: $Data2 = Capsule::select(Capsule::raw('SELECT value FROM tbladdonmodules WHERE module = "ctaa" AND setting IN ("text") LIMIT 1')); foreach ($Data2 as $v2) and so on 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted May 4, 2020 Share Posted May 4, 2020 From what I can see, you are performing 3 queries on the same table to get 3 values. This is inefficient for 3 reasons: One query is better than multiple ones WHERE a = a is faster than IN (a) Looping through an array that contains one element is unnecessary With the query I have previously posted you can get all 3 values in a single query. There's no need to "overload" your database with 3 queries and your PHP for performing unnecessary loops. 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted May 4, 2020 Share Posted May 4, 2020 7 hours ago, Kian said: With the query I have previously posted you can get all 3 values in a single query. There's no need to "overload" your database with 3 queries and your PHP for performing unnecessary loops. I absolutely agree - three separate queries to the same table, each only pulling one value is not a good idea. 0 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.