Jump to content

Cant fetch data from database


Recommended Posts

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?

Link to comment
Share on other sites

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'); 

});               

 

Link to comment
Share on other sites

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 🤮

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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. 🙂

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

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