I have a function that takes 3 arguments
1 param: is a string, 2 param: a variable, 3 param: database cols values
The way i want to use this function is:
add_column('status', ('$1'<time() and '$2'>time()) ? "active" : "inactive", 'pack_activated_on,expire_on')
and every time it returns inactive.
If i use values instead of $1 and $2:
add_column('status', '1502197610'<time() and '1533675600'>time()) ? "active" : "inactive",'pack_activated_on,expire_on')
works (it return active).
The function return the corect values for the 2 params $1 and $2 (ex:)
add_column('status', '$1','pack_activated_on,expire_on')
i get the value of pack_activated_on = 1502197610
and the same for $2
add_column('status', '$2','pack_activated_on,expire_on')
get the value for expire_on = 1533675600
What i am doing wrong here?
EDIT
I don`t know why people give dislike, maybe the question is unclear? Just say and i will try to change the question!
Anyway....that function add_column is from this class datatable class which using datatable jQuery plugin server side
you are giving your variables to the function as string literals. so your conditional has to avaluate string
just remove the single-quotes around your variables $1
/ $2
Contents inside of single quotes '
are interpreted literally. What this means is you are literally comparing the string $1
to time()
.
There's no reason at all to wrap your variables in quotes in the context you have provided.
Also, as pointed out by Justinas, PHP variables cannot start with a number. So you should rename your variables to something meaningful.
So, solution is: just do e.g. $variable_formally_known_as_1 < time()
.
Problem is in pure PHP. You are comparing literal '$2'
with integer from time()
. So PHP converts '$2'
to integer 0, and compares with time() value - that is always false (0 > 1533675600
).
You need to extract your '$1'
to actual parameter instead of using reference to that (sorry, I do not know CodeIgniter for this part)
I read the documentation for that function carefully and now works :)
function status($var1, $var2){
return ($var1<time() and $var2>time()) ? 'active' : 'inactive';
}
and in the function:
add_column('status', '$1', 'status(pack_activated_on, expire_on)')