How to use tablepress data with PHP script
-
Hi,
How can I access to a specific cell in a tablepress table using a PHP script?
Cheers
Thierry
-
Hi,
thanks for your post, and sorry for the trouble.
For this, you can use the
load()PHP method, which is part of theTablePress_Tableclass, e.g.<?php $table_id = '123'; $table = TablePress::$table_model->load( $table_id ); // $table['data'] is now a two-dimensional array $cell = $table['data'][3][4]; // This is the fifth column in the fourth row (zero-based indexing).Regards,
TobiasHi,
Thank’s for your answer.
I suppose I’ve to include specific file class..because I got this message when I use your script.
” Access to undeclared static property: TablePress::$table_model”
Thierry
Hi,
ah, my bad. That line should be
$table = TablePress::$model_table->load( $table_id );Sorry for the confusion.
Regards,
TobiasThanks ..it’s work.
I’ve another question. I found how to count the number of rows with something like count( $table[‘data’]) but I don’t find the equivalent for the number of columns?
Thanks for your support
Thierry
Hi Thierry,
to count the number of columns, you’ll have to count the number of cells e.g. of the first row:
$number_columns = count( $table['data'][0] );Regards,
TobiasHello,
A bit late in the topic line, but I installed the PHP in cell plugin. It is working with simple operations like a direct echo, but I want to display an image (echo the) if a certain cell has a value grater than 0.
For this, I tried using the following statement to echo at least if it is gt than 0 or not, but I get a blank cell (my table id is 23, it has 9 columns and many rows – i think it might be difficult to load) :<?php
$table_id = ’23’;
$table = TablePress::$model_table->load( $table_id );
$cell = $table[‘data’][3][6];
if ($cell > 0)
echo DA;
else
echo NU;
?>I don’t know if there is something i’m doing wrong, or what.
Hi,
it seems that you are echoing constants and not strings. DA and NU have to be wrapped in quotation marks:
<?php $table_id = '23'; $table = TablePress::$model_table->load( $table_id ); $cell = $table['data'][3][6]; if ($cell > 0) echo "DA"; else echo "NU";Regards,
TobiasHello, even with the quotes (yes, it is right- i forgot i wasn’t using variables, but strings) it still doesn’t echo anything.
Tried using another cell (i don’t know if the column is also numbered from ‘1 to n’ or from ‘0 to n-1’).
Still nothing.
What if it’s an incompatibility? I am using wordpress 4.6.1. Haven’t updated yet to 4.7..
Thanks and happy holidays!Hi,
indeed rows and columns are number from
0ton-1in the array.
What do you get when you
echo $table['data'][3][6];
? That should give you the correct value.Regards,
TobiasFigured it out.
Started from 0 again. All the statements that were written on multiple lines, were ignored completely. So i tried echoing a variable – all good. Then all the code above written on the same line – it is working.
I don’t know why it won’t work on multiple lines, but..
Thank you so much for the prompt answer.
Berta T.Hi Berta,
great! Good to hear that it’s working now 🙂
You could try turning off the automatic conversion of linebreaks, by extending the used Shortcode to
[table id=123 convert_line_breaks=false /]
With that, it might be possible to write the code in multiple lines.Regards,
TobiasHi Tobias,
Newbie question however in order to call the line
$table = TablePress::$model_table->load( $table_id );do I need to be in a particular context, should the variable $model_table be set somewhere?
Hi,
yes, that variable needs to be set, which happens automatically on both frontend and admin requests.
You will basically just have to make sure that your code runs after theinitaction hook of WordPress runs.Regards,
TobiasThanks Tobias! In my scenario I want to run my PHP script behind the scenes via cronjob (to update the table with new rows). Therefore the init method will not have been called unless. Is it possible or a good practice to call the init method in this context or is there a better approach you could recommend.
Hi,
even when running your own PHP script, it’s probably wise to do this through the WordPress API functions, i.e. by letting WordPress handle the cron request.
Otherwise, if you don’t call theinithook anywhere, certain things like WordPress authentication will not have been set up, and most plugins will not have been loaded and initialized.You could maybe also take a look at the TablePress Extension from https://tablepress.org/extensions/table-auto-import/ which also uses cron (the WordPress Cron system, however).
Regards,
Tobias
The topic ‘How to use tablepress data with PHP script’ is closed to new replies.