• Resolved cousinr

    (@cousinr)


    Is there any way to retrieve data from a specific cell in one of the tablepress tables?

    Updated

    I tried creating a custom shortcode that accesses a specific cell of a specific table. I’m not sure if the approach is correct, but I was able to get the data.


    function load_specific_cell_data($atts) {
    // Define shortcode attributes
    $atts = shortcode_atts(
    array(
    'table_id' => '', // ID table
    'row' => '1', // Line number (numbering from 1)
    'column' => '1', // Column number (numbering from 1)
    ),
    $atts
    );

    // Check that the table ID is passed
    if (empty($atts['table_id'])) {
    return 'The table ID is not specified.';
    }

    // Check that TablePress is active
    if (class_exists('TablePress_Table_Model')) {
    // Load the table model
    $model_table = new TablePress_Table_Model();

    // Load table by ID
    $table = $model_table->load($atts['table_id']);

    // Check that the table exists and contains data
    if (is_array($table) && isset($table['data'])) {
    // Convert row and column numbers to indices (numbering from 0)
    $row = intval($atts['row']) - 1;
    $column = intval($atts['column']) - 1;

    // Check if there is data in the specified row and column
    if (isset($table['data'][$row][$column])) {
    // Return cell value
    return esc_html($table['data'][$row][$column]);
    } else {
    return 'No data found in string ' . ($row + 1) . ', column ' . ($column + 1);
    }
    } else {
    return 'Table with ID ' . esc_html($atts['table_id']) . ' was not found or does not contain any data.' ;
    }
    } else {
    return 'TablePress is not installed or active.';
    }
    }

    add_shortcode('get_table_cell', 'load_specific_cell_data');
    • This topic was modified 6 months, 2 weeks ago by cousinr.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi!

    Thanks for your post and sorry for the trouble!

    Yes, in principle, your approach and from what I can see code is totally correct!

    You could also compare this to the existing solution from https://tablepress.org/extensions/table-cell-shortcode/

    Best wishes,
    Tobias

    Thread Starter cousinr

    (@cousinr)

    Hello,

    I took a look at the article, https://tablepress.org/extensions/table-cell-shortcode/.
    and I can tell you it’s not exactly what I was looking for. I had a slightly different task in front of me. I needed to “get data in a cell of one table from some cells of another”. I apologize for formulating the problem incompletely right off the bat.

    Thanks

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    But that’s what this Extension does ?? Just put that Single Cell Shortcode into a table cell to retrieve data from another table. The code is pretty much the same as in your solution.

    But what matters in in the end is that you found a working solution for your problem, of course!

    Best wishes,
    Tobias

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How get data from specific cell tablepress’ is closed to new replies.