I'm using CodeIgniter and I came across an interesting problem. I need to use the variables from one function on another. I was planning to do this by simply declaring global variables (which I wasn't able to) do in the framework. So I tried calling one function from within another (this is all happening in the controller). Since apparently this can't be done I made a helper file with the common function and then just tried to load it but I get this error:
Fatal error: Call to undefined method ReporteNominas::getValues()
the helper file is inside the helpers folder and it contains this:
function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal){
$totalPares = $tpar;
$ventasPiso = $vpiso;
$totalComisiones = $tcomi;
$totalGastos = $tgas;
$totalTotal = $ttotal;
if($getThem){
return $totalPares . "," . $ventasPiso . "," . $totalComisiones . "," . $totalGastos . "," . $totalTotal;
}
}
and I'm trying to call it doing this:
$this->load->helper('helper_common_functions_helper');
$this->getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'], $query['tot']);
what could I be missing here?
Try this:
$this->load->helper('helper_common_functions_helper');
getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'], $query['tot']);
A helper (if done properly) is just a group of functions, NOT a class, so you can call it as a regular function call.
You should also do this, in your helper:
if ( ! function_exists('get_values'))
{
function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal)
{
//rest of code
}
}
To avoid a 'redeclare function' error when loaded more than once
Helper's are just functions so instead of calling them like a class with $this-> you just call them like a normal php function. So change this
$this->getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'],$query['tot']);
to this
getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'],$query['tot']);
Related
I know we can minify HTML in CI3 through the hook but in CI 4
I have done it by adding a minify function before return on every method.
minifyHTML(view('admin/template/template',$this->data));
Any other way I can do it without using the minify function everywhere?
I also figure out another solution which is to add a template function in BaseConroller which renders all the views. but I have already used view() in many places in the project and its not feasible for me but can be work for others.
In CodeIgniter4 You can use Events to Minify the output (Instead of using Hooks like what we did in CodeIgniter3) , by adding the following code inti app/Config/Events.php file
//minify html output on codeigniter 4 in production environment
Events::on('post_controller_constructor', function () {
if (ENVIRONMENT !== 'testing') {
while (ob_get_level() > 0)
{
ob_end_flush();
}
ob_start(function ($buffer) {
$search = array(
'/\n/', // replace end of line by a <del>space</del> nothing , if you want space make it down ' ' instead of ''
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/' //remove HTML comments
);
$replace = array(
'',
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
});
}
});
see https://gitlab.irbidnet.com/-/snippets/3
You need the extend your core system classes to be able to do that in a system wide scope.
Every time CodeIgniter runs there are several base classes that are initialized automatically as part of the core framework. It is possible, however, to swap any of the core system classes with your own version or even just extend the core versions.
Two of the classes that you can extend are these two:
CodeIgniter\View\View
CodeIgniter\View\Escaper
For example, if you have a new App\Libraries\View class that you would like to use in place of the core system class, you would create your class like this:
The class declaration must extend the parent class.
<?php namespace App\Libraries;
use CodeIgniter\View\View as View;
class View implements View
{
public function __construct()
{
parent::__construct();
}
}
Any functions in your class that are named identically to the methods in the parent class will be used instead of the native ones (this is known as “method overriding”). This allows you to substantially alter the CodeIgniter core.
So in this case you can look at your system view class and just change it to return the output already compressed.
In your case You might even add an extra param so that the view function can return the output either compressed or not.
For more information about extending core classes in CodeIgniter 4 read this:
https://codeigniter.com/user_guide/extending/core_classes.html#extending-core-classes
In CodeIgniter 4, you can minify the HTML output of your views by using the built-in output compression library.
First, you need to enable output compression in your application's configuration file, which is located at app/Config/App.php.
Then, you need to set the compression level. You can set the compression level by changing the value of compress_output option. The possible values are:
0 : Off (Do not compress the output)
1 : On (Compress output, but do
not remove whitespace)
2 : On (Compress output, and remove
whitespace)
Finally, you need to load the Output Library in your controller, before the output is sent to the browser. You can load it using the following line of code:
$this->load->library('output');
I want to make use of functions to get the full path and directory name of a script.
For this I made two functions :
function _jb-get-script-path ()
{
#returns full path to current working directory
# + path to the script + name of the script file
return $PWD/${0#./*}
}
function _jb-get-script-dirname ()
{
return ${(_jb-get-script-path)##*/}
}
as $(_jb-get-script-path) should be replaced by the result of the function called.
However, I get an error: ${(_jb-get-script-path)##*/}: bad substitution
therefore i tried another way :
function _jb-get-script-path ()
{
return $PWD/${0#./*}
}
function _jb-get-script-dirname ()
{
local temp=$(_jb-get-script-path);
return ${temp##*/}
}
but in this case, the first functions causes an error : numeric argument required. I tried to run local temp=$(_jb-get-script-path $0) in case the $0 wasn't provided through function call (or i don't really know why) but it didn't change anything
I don't want to copy the content of the second fonction as i don't want to replicate code for no good reason.
If you know why those errors happen, I really would like to know why, and of course, if you have a better solution, i'd gladely hear it. But I'm really interessed in the resolution of this problem.
You need to use echo instead of return which is used for returning a numeric status:
_jb-get-script-path() {
#returns full path to current working directory
# + path to the script + name of the script file
echo "$PWD/${0#./*}"
}
_jb-get-script-dirname() {
local p="$(_jb-get-script-path)"
echo "${p##*/}"
}
_jb-get-script-dirname
I am wondering if it is allowed to access/get/use the variable from one function to another function within one controller?
Thank you.
Try Something like this.
class Index extends CI_Controller {
protected $var1;
public function index() {
$this->setVar1();
echo $this->var1; // print $var1
}
public function setVar1() {
$this->var1 = 1;
}
}
$superduper = 'superduper' ;
available only locally within the method (function)
or passed like
$this->reviews->insertToReviews($superduper) ;
versus
$this->superduper = 'superduper' ;
as soon as $this->variablename is declared in a controller its instantly available to:
any method in the controller
any method in any model that is called by the controller
any view file called by the controller
so without passing the variable - in a view you can use
echo 'my lunch is ' . $this->superduper ;
but often its better to explicitly pass values to the view especially if they are unique to the method - it makes it easier to see what is going on. so in that case in controller:
$data['superduper'] = $this->superduper ;
and in view
echo 'my lunch is ' . $superduper ;
Now when anyone looks at the method in the controller - we can see that superduper is being passed to $data. the point is that even though you can avoid passing variable names to methods or the view by declaring $this->somename - if you pass them locally it can make it easier to see what is going on.
The flip side is something like:
$this->error_message = "Error retrieving database records";
is awesome. you can have error messages in any method and they will be automatically available no matter what else happens. so then in your view file have something like
if($this->error_message != '') { echo $this->error_message ;}
this is especially helpful while you are building the site.
I am making a jquery plugin.
I am trying to create a jquery plugin,
I am attempting to add the ability for users the run a function at certian times.
so, in the option array the user could have
{created: function(){alert('created called!');}}
Now in the plugin,
How do i run that code?
i tried just
options.created and eval(options.created);
but neither have any effect.
What am i doing wrong?
Have you tried options.created()?
More info: When you set a variable to an anonymous function in JavaScript, you must call the variable the same way you would any "regular" function. This allows you to pass parameters to the function.
For example:
var aFunction = function(a, b) { alert(a + " " + b); };
aFunction('Hello', 'world');
I am having a little bit of difficulty passing a variable into a selector in prototype. I would like to be able to pass a variable into the select string, so that one function can work for many of the same kind.
At the moment, this is what I would basically like to do:
function myFunct(var)
{
$(var + 'add_form').hide() //so inde the brackets would be ('#product1 #add_form') for example.
}
Be able to pass 'var' into the function that would pass it to the selector, so that I can hide a pattern that is the same for many on the page.
Any ideas for a path to follow would be greatly appreciated.
You're on the right track! Couple things:
var is a JavaScript keyword (source), don't use it to name a variable
if you're querying an element by id (such as #add_form) you don't need to add any container element as you're doing
If you're querying an element by class, you need to use the $$ function, not the $ function
You need to iterate over the wrapped set to call your method
whitespace is significant in css selectors, so make sure to include those in your selector construction to tell Prototype to search within your parent container:
function myFunct(parent) {
$$(parent + ' .add_form').invoke('hide')
}
myFunct('#someparent'); // hides .add_form inside #someparent
That should work... just rename var to something else.
function myFunct(yourVar)
{
$$('#' + yourVar + ' .add_form').each(function(s){ s.hide(); }); // yourVar being the id of the container element
}
I've put a '.' in front of add_form because you can't use multiple elements with same ID, make it a class.