The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.1.2-1ubuntu2.14 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once



UserSpice
function didn't work - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Support Center (https://userspice.com/forums/forumdisplay.php?fid=23)
+--- Forum: UserSpice 4.3 and Below (https://userspice.com/forums/forumdisplay.php?fid=26)
+--- Thread: function didn't work (/showthread.php?tid=705)



function didn't work - Katronix - 08-31-2017

Hello all from Soggy Houston, TX!

I tried this function:


function getConfig ($search='')
{
require_once 'users/init.php';
require_once $abs_us_root.$us_url_root.'users/includes/header.php';
$config = $db->query("select options from config where setting = ?",array($search));
$back = $config->first()->options;
return $back;
}

and received the following errors:

Notice: Undefined variable: abs_us_root in C:\xampp\htdocs\follower\function.php on line 7

Notice: Undefined variable: us_url_root in C:\xampp\htdocs\follower\function.php on line 7

Notice: Undefined variable: db in C:\xampp\htdocs\follower\function.php on line 8

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\follower\function.php:8 Stack trace: #0 C:\xampp\htdocs\follower\index.php(11): getConfig('logo') #1 {main} thrown in C:\xampp\htdocs\follower\function.php on line 8

This was to the call of: $set = getConfig('logo');

Can anyone point me where I went wrong?


function didn't work - Brandin - 08-31-2017

Where did you make this function at? And why is your init call in it? You shouldn't need to call your init or your header in a function.


function didn't work - Katronix - 09-01-2017

I have a file of just functions. I had read https://userspice.com/documentation-db-class/ perhaps I misunderstood it. I did replace it with $db = DB::getInstance(); and it worked fine.


function didn't work - mudmin - 09-01-2017

Usually these sorts of functions go in usersc/custom_functions.php

I think the function would look more like this...
function getConfig ($search)
{
$config = $db->query("select options from config where setting = ?,array($search));
$back = $config->first();
return $back;
}

Then you would call it by saying
$options = getConfig('myoption');

I'm guessing your config table is setup with basically 3 columns..
id(auto incrementing)
options (the actual options in varchar)
setting (whatever you call the options in varchar)


function didn't work - mg0411ph - 03-04-2018

Going to hop in on this thread. I also need help with using Class variables on the usersc/custom_functions.php

Same with OP, I had to use $db = DB::getInstance(); but I'm now having issues with the notification.

Is there an easier way to get the functions to call Class variables?


function didn't work - dan - 03-04-2018

Functions are sort of like standalone programs. If you want them to have access to things outside them, you usually have to bring it inside the scope of the function. That means instantiating the class, calling the function, or using a global. For instance... Iirc, you can do global $db; to use the db class that was already instantiated in the header. This can get weird if you reuse that variable name somewhere else on the page.