How to run a function within the $columns array
How to run a function within the $columns array
erbanach
Posts: 18Questions: 5Answers: 0
I need to update the $columns array to run PHP functions on the data in this array. This is what my $columns array looks like right now:
$columns = array(
array( 'db' => 'Client', 'dt' => 0 ),
array( 'db' => 'EstimateNumber', 'dt' => 1 ),
array( 'db' => 'Status', 'dt' => 2 ),
array( 'db' => 'CurrentEstimateTotal', 'dt' => 3 ),
array( 'db' => 'CreatedDate', 'dt' => 4 )
);
I need to update the CurrentEstimateTotal to add the money_format() PHP function. I also need to use the date_diff() PHP function on the CreatedDate column. Please let me know how to add this functionality in. Thank you!
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
The demo SSP class has a
formatteroption in addition to thedbanddtoptions in the column array definitions. This is basically a function (that you define) which will be called with each value for that column and format it as required.There are three examples of that in this example. The
idcolumn adds a prefix string, thestart_datecolumn uses a date formatter and thesalarycolumn uses a number formatter.It is important to note that the SSP class is designed for the 80% use case only. It is primarily a demo and doesn't allow complex interactions such as joins, grouping and SQL functions. However, if you don't need them, it can be super useful for setting up a quick server-side processing table!
Regards,
Allan
Thank you. I have edited my columns array to the following (I have only edited the CreatedDate column):
It is outputting nothing. No errors. Is there something wrong with my implementation? Thanks for your help!
If you have a look at your server's error logs I suspect you will find an error related to
$current_datesince it is being used in a function that doesn't directly have access to that variable. You need to use:The PHP documentation has full details about this if you are interested in more information.
Regards,
Allan
Hi Allan! Please forgive me for this long-winded post.
I have run some tests, and have a function working as intended outside of the DataTables
$columnsarray. My goal is toechoa count of the number of days in between today's date and the CreatedDate column in mySQL database in my DataTables column.This function works as intended, and outputs an
echoof the number of days between$current_dateand$dateas: "16 days live", "15 days live", etc.In my DataTables
$columnsarray, I am trying to achieve the same effect:By and large, these functions are very similar. But I keep receiving an error of:
Which I believe is in reference to either the
$created_dateor$datevariables.Why am I receiving this error, and how can I fix this? I am having a hard time getting a
var_dump()of the$created_dateand$datevariables within the DataTables columns to see what is the problem, which lead me to do some testing outside of the DataTables$columnsarray. Am I missing something? Thank you in advance for your help!I got this working! I simply needed to
returnthe result instead ofecho. Thanks for your help, Allan!Two issues I can see:
1:
echo $diff->format('%d days live');Don't echo it. Return it. Let the SSP class do the echoing.
2:
$created_dateis being used inside the closure function. Just like$current_dateit needs to be defined in theusestatement, per the PHP documentation. I'm slightly surprised the error is ambiguous about that = what is the line number the PHP error gives?Allan
After I included the
returninstead ofecho, it started working!