Okay
  Public Ticket #3355351
How to read tables programmatically
Closed

Comments

  • David Leigh started the conversation

    Hi,

    I may be thinking about this problem incorrectly - i.e. with respect to how I think I could use wpDataTables.

    What I want to do: 

    I would like to store data in a wpDataTables table for easy updating in the WordPress backend.  I'm not (initially) looking to display the table, but I would like to access it programmatically from a plugin or PHP code snippet in order to extract specific field values for use in different website functionality.  

    In all the filters and hooks I'm seeing, I'm not seeing basic ways to programmatically access the data in the tables.  Could you point me towards any documentation that would help me or a better approach?

    Thanks!

  •  1,708
    Miloš replied

    Hi, David.

    I am not a developer, but will do my best to try and provide any kind of examples.

    Not sure if i understood the use-case.

    Our support is very limited when it comes to custom solutions like this.

    - I am not sure if you need this exactly, but I will share it with you and let me know if that helps.

    1. This example was advised by our devs, if a user wants to make a custom back-end page where you can access the tables data.

    You can use function wdtOutputTable which is Helper func that prints out the table in backend. 

    Also you have to include all modals(delete, close and edit modal template)

    <!-- /Modals -->
    <?php wdtOutputTable('1'); ?>
    <!-- Delete modal -->
    <?php include WDT_TEMPLATE_PATH . 'common/delete_modal.inc.php'; ?>
    <!-- /Delete modal -->
    <!-- Edit modal -->
    <?php include WDT_TEMPLATE_PATH . 'frontend/modal.inc.php'; ?>



    2. This user wanted to convert the values in one of the columns to a link that when clicked will run another query passing in the value clicked as a variable in MySQL and then display a second table with the new data.

    -

    This is how we first advised him, to construct the column by using CONCAT with our SQL Query based tables :

    if there's a page on your website, that contains a string from a column (for example "page1" ), you can use CONCAT function, like:

    SELECT column1,
            CONCAT('<a href="www.yourwebsite.com/',yourTable.column2,'/">',yourTable.column2,'</a>') AS LINK
    FROM yourTable
    

    That would append "page1" found in column 2 to www.yourwebsite.com/, so the generated link would be www.yourwebsite.com/page1/, hyperlinked behind "page1" - entry in the cell.

    -

    The user then installed "Insert PHP Code Snippet", and this is what he said :

    "Using that plugin, I created a shortcode that look like this:

    Shortcode Name: GetReport

    <?php echo do_shortcode('[wpdatatable id=4 var1="' . $_GET['ID'] . '"]'); echo do_shortcode('[wpdatatable id=3 var1="' . $_GET['ID'] . '"]'); ?>

    Then in my page where the table is generated, I insert the shortcode below to display the tables using the ID param from the URL:

    [xyz-ips snippet="GetReport"]

    Hopes that can help the others looking for a solution".

    -

    Let me know if something like this might help, if you are trying to achieve something similar?

    Or if you need something completely different, 

    for example when you said you need to "programmatically access the data in the tables",

    and "extract specific field values for use in different website functionality."

    (The only way i know to extract single cell's data anywhere on the site,

    is with our built-in featureSingle Cell Shortcode.

    If you haven't already, you can check this out as well, might be useful.)

    If you can elaborate about those two points in more details,

    and we can ask our developers for advice, if they can provide any working examples to get you started?

    Thank you.

    - If you can provide any working examples with screenshots or record a Video for us, that would be good.

    If it contains any sensitive data, just send the reply as PRIVATE, for security reasons.

    -


    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • David Leigh replied

    Thank you so much for the response.  This is helpful and gives me some ideas.

    Yes, my use-case was pretty vague - mostly because it is a bit complex.

    To hopefully shed a bit more light on it here's some more detail: 

    • First off, while wpDataTables seems clearly focused on front-end displays of tabular data, I'm looking MOSTLY at its ability to be an easy-to-use interface for signed-in administrative users to enter and update tabular data.
    • That data then needs to be displayed on the front end, but not as a table.
    • To be specific, each table row will correspond to a motor racing event with specific information for the event (dates, times, event names, location, etc.) and statistics for an individual driver (practice times, qualification times, race position, etc.)
    • While the possibility exists in the future to display some of the data in a front-end table, the primary display will take individual columns from the row for the current event and display and/or use that data in different places (e.g. a count-down timer based on race start date/time, etc.)  Consequently, I need to be able to extract a single column from a specific row.
    • The actual display of data MIGHT happen via a shortcode OR, in some cases, a shortcode is not available to be consumed (like for the count-down timer, for example), BUT a custom field can be used.  Consequently, I need to read the specific column from the specific row in the wpDataTable and then put it in a specific custom field on a specific page for it to be displayed correctly on the front end.
    • These will likely be PHP code snippets that will run via a cron job or wp-cron.
    • I will also need to pull race statistics from an online service in a JSON file and then put that data into specific columns on the row for the current race weekend.

    I think that might give you a better summary of what I'm trying to do and how I hope to use wpDataTables.  That might not be a primary use-case for wpDataTables, but I certainly don't see it as being wildly strange.  For populating the table with statistics, I might do well to use a MySQL-based table and just do the population with INSERTS and UPDATES instead of going through wpDataTables.  But for the rest, I would really like to use wpDataTables where it makes sense.

    Thanks!

  •  1,708
    Miloš replied

    Hi, David.

    You're welcome, we will do our best to advise you, as much as our support is able to cover.

    1. In regards to easy-to-use interface that Admin users can use in order to edit tables,

    the only editable tables we have can be Manual Tables ,  and SQL Query-based Tables.

    Manual tables can be made from scratch, or source data can be imported from CSV/Excel/Google Sheets in order to make a new Manual Table;

    and existing Manual Tables can be updated via file upload from CSV/Excel/Google Sheets as well.


    We also have a feature Excel-Like  Editor, only for Manual Tables,

    which can be useful in some situations for quicker editing.


    2. In regards to displaying that data on front-end, but not as a table.

    The only built-in way that we currently have from our plugin to pull data from cells outside of the table,

    is with our Single Cell Shortcode feature,  which can allow you to place specific cell(s) pulled anywhere on the site.

    -

    Alternatively, for the other methods you described, such as pulling the table data via PHP code snippets that will run via a cron job or wp-cron;

    and the solution for a count-down timer;

    at this moment we did not have time to try anything like that yet,

    so we don't have working examples to show you.

    -

    At this time, we don't have a built-in solution to achieve this, but you can suggest it to our developers - they will do their best to make a solution in the future.

    Please feel free to search on our suggestions page

     to see if someone may be already suggested this feature. If you can't see it, feel free to add your suggestion there,  and as more people vote, the feature will move higher on the priority list.

    You can certainly follow our changeLog page if you'd like ( it is also available in the plugin dashboard), where we state any changes/new features/bug fixes during updates;

    and our newsletter, so you're informed about new features, bug fixes, freebies, etc.

    -

    If you have coding skills and wish to try to make a custom solution now,

    you can check out our available hooks for Developers on this documentation and see if you can find any hook that might help.

    Please be advised that custom solutions with hooks are not included in our support.

    You can also research resources such as Stack Overflow to see if any other user perhaps found a workaround.

    ( We do like to give examples for certain solutions, but for this use-case, we, unfortunately, don't have anything yet)

    If you find a hook that might help and if that hook does not behave as expected, our devs can try to advise if it does not require custom work, if perhaps just a basic correction is needed.

    -

    Additionally, i will share one custom workaround from another user , who needed a different use-case,

    but i hope that it might give you an idea what you could try.

    That user's specific use-case was like this :

    'After user adds a new entry as a string in table cell I want to automatically that string to be send to an API url. That API returns a JSON. I want that JSON to be stored in the same table, another cell'.

    Our developers advised him, like this :

    You can use this hook   wpdatatables_filter_formdata_before_save

    //$formData array of entries from edit modal
    // $tableID table id int
    function updateValuesBeforeSave($formData,$tableID){ 
    //for specific table id 
    if ($tableID == 1) { 
    //Your logic to send data to API URL from for example column that have orig_header col_1 
    // you will use $formData['col_1'] 
    // then response from your API URL you will insert on anoter column that you need for example if column is 
    // called col_2 then you will insert response of that API like this 
    // $formData['col_1'] = 
    //response from your API URL 
    // then that data will be saved in database and also shown in your table on front } return $formData;
    }
    add_filter('wpdatatables_filter_formdata_before_save','updateValuesBeforeSave', 10, 2);

    I hope that might help at least as an idea of something similar that might be modified for your use-case.  


    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables