Okay
  Public Ticket #3671468
filter tables with user data
Closed

Comments

  • steven wolff started the conversation

    I need to filter tables using the users id and other data so the user sees their data only.

  • steven wolff replied

    I still need help with filtering a table using the user id or username. I don't see any ex amples of how to filter using a variable. I know SQL, so If there is a place I can add the SQL code to filter that would be great. Please help. Your documentation said you give support.


    Thanks,

    Steven Wolff

    [email protected]

  •  1,846
    Miloš replied

    Hi Steven,

    Firstly, my sincerest apologies, there was an error with this ticket.

    I wrote a reply but it did not get sent.

    This will not happen again, we will monitor all open tickets closely going forward.

    Thank you very much for all your patience.


    In regards to filtering tables per user id, we have a couple of different solutions.

    I will share them and you can try them out to see what would work best.

    1. If you use editable tables, please check out this feature which is easy to use, it will limit the rows for each user so they can only see the rows associated to their id : https://wpdatatables.com/documentation/front-end-editing/allowing-users-to-edit-only-their-own-data/.


    2. Or, you can use our predefined filters and dynamic placeholders, which can also be combined with a "WHERE" SQL statement.

    Here are some examples :


    1. If you wish to try Pre-filtering tables through URL,  please check out this page with examples.


    You can also use placeholders for table generation.

    1. For example, if you have a MySQL query-based table, you can use this query:

    SELECT * FROM yourDatabaseTable
    WHERE columnName = '%VAR1%'

    %VAR1% can be defined in the Placeholders tab when you're creating a table, 

    or you can pass a different VAR1 value in the table's shortcode.


    2. Or, if you don't add the condition in an SQL query ( or don't use an SQL Table),

    you can add %VAR1% in column settings -> Filtering, under "Predefined filtering value".

    Then, you can have different versions of the same table, with the VAR1 value passed in the shortcode,

    let's say if we had a "Regions" column and we wish to filter a different region per each page.

    9385158392.png

    If we want page 1 for example only filtered for "Region A" , you can set predefined filter to this column as %VAR1% (filter type has to be set as text) :

    4266306375.png

    Then for a page where we need only Region 1 filtered, use shortcode [wpdatatable id=1 var1='Region A'] ,

     page 2 for only "Region B", shortcode would be [wpdatatable id=1 var1='Region B'] and so on, 

    to apply the values defined in the shortcode to the filter.

    ( This filtering method can be used for any table type, besides Simple Tables)

    Let me know how it goes and if you have any additional questions.

    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    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

  • steven wolff replied

    I know SQL , but I don't know the table name of the wpdatatable that was created from the  formidable form. and don't know where to put the SQL. do I still use the formidable form as a datasource? I don't want to use the URL variable option since it exposes the process.


    Thank you,

    Steve

  •  1,846
    Miloš replied

    Hi Steve,

    It will not be easy to pull data via SQL from a Formidable Form.

    Especially if you make a table in our Plugin linked from Formidable Form, then you wish to make an SQL Query from that, but we will try to clarify how all that works.


    1. When you make a table in our Plugin which is linked to a Formidable Form - this does not make a new table in your SQL Database.

    These types of linked datatables are not saved in the database like manual tables, but instead, their data is stored in database table wp_wpdatatables_rows. 

    So, pulling data from the database is a lot more complex because this database table stores data in JSON format, not like manual tables (columns * rows).

    So the entries/submissions/data itself, is still being stored in the original Formidable Form, while our Table only pulles the entries data and shows them in our Table.


    If you simply need to show data from a Formidable Form and filter this for each User ID ( only show the entries which the current user added), here is a workaround guide to achieve it :


    Regarding filtering a Formidable table for currently logged user ID ( to show only entries which that user submitted),

    i will show an example workaround, and also how you can use CSS to hide the column filters if you wish to prevent users from "clearing the filter" and see other people's rows.

    -

    First we need to add a hidden Field in the Form to store the "user ID" information.

    5146824406.png

    -

    I will show you a working example for a table connected with Forminator Form,

    but you can use absolutely the same principle for the table connected to a Formidable Form.

    Here is my 'test example' with just a very basic Form and one Text field.

    Since we already added that hidden "user ID" field in the Form,

    i just added a couple of submissions.

    The first one by my Admin user, which has user ID of 1;

    then i log as another user, with user ID of 11, and add another submission.

    By default, i have just left the field name without setting it up at the Form, and the table picks up this field as origin header of "hidden1".

    6733523157.png
    6107479450.png

    We can change the Displayed Header name to anything, i name it "user ID",

    and of course, you can hide that column from front-end users if needed, i just left it for demonstration purpose.

    -

    Now, i will explain how you can use this user ID field to pre-filter the table for front-end users,

    and also you can hide the filter with CSS - to prevent any user to "clear the filter" in order to see everyone's subbmisions.

    -

    This is a detailed example of using an Excel table - but you can use absolutely the same principle/logic for table linked with your Forminator Form.

    - On this Excel, we added a custom "user_id" column on the source Excel side, which you can hide if needed in our plugin from the front-end.   ( On your Form, you will already have that hidden user ID field as mentioned above);

    We will add a predefined filter value to that column, using a PlaceHolder "Current User ID";

    - Check that the filter type is set as "Text", as shown on this screenshot.

    8249361981.png

    - Here is how an example table linked to a source file looks before we make any changes, with visible filters above the table.

    7932387639.png

    And now, when we set the pre-filter, with Administrator logged in ( In our case, the user ID is 1), we only see rows matching that user ID.

    3633379684.png

    - (If you need more detailed information about using Place Holders in wpDataTables, here is our documentation about that https://wpdatatables.com/documentation/table-features/using-placeholders/);

    - The user_ID filter is still visible, which means, if someone changes/deletes this value, they could see the whole table.

    We will hide it from the front-end with some custom CSS added in the "Customize" section of the main table settings.

    - It should be easy to do, depending on your knowledge of HTML and CSS.

    You have to find the correct selector using the browser inspector tool, add the style "display:none" property to it, and it will disappear from the front-end.

    On our example table, the custom CSS is :

    #table_1_5_filter_sections  {
        display:none; }
    5537273103.png

    - Now, the user_ID filter is gone from the front-end.

    8120839900.png

    - Optionally, you can hide the user_id column from the front-end, if needed, either using the column settings tab or the "column list" button from the back end Admin area.

    1618300341.png

    - If you need more details about this, don't hesitate to contact us further.


    So that is the solution to filter rows in a table linked from the Formidable Form, then they only see the rows associated to the currently logged user ID.


    2. On the other hand, if you must pull an SQL Query from Formidable Data, as mentioned, this is not an easy task, it can only be done with a custom SQL Query based table.

     and our Support does not cover it, because 

    Please note: Using this plugin feature requires at least a basic knowledge of SQL. It is assumed that you can create the table in some MySQL data manager (e.g., PHPMyAdmin, MySQL Workbench), and prepare a SQL query that will return the data you need. 

    A good way to achieve this can be experimenting using our WPDB SQL Query Constructor tool.

    You can also try this other SQL Query helper Tool,

    Create a MySQL-query-based table by querying a database,

    that one has a wider range of all tables you can choose from your Database outside of WPDB.

    - Please note: this tool is not an ultimate query generator. It simply constructs a suggestion of a query. We are constantly working to improve it, but SQL is such a complicated and flexible language that full automation for constructing queries is next to impossible. 

    Consequently, the more complicated the query, the higher is the probability that it will not return exactly what you need. So, you will often need to play around with the resulting query.

    Please be advised that writing custom SQL Queries or debugging Queries does not fall under what our support covers.

    You can check out this official page of WP codex,  for help with writing custom Queries.

    If you manage to get a Query working  directly in your PHPMyAdmin ( or any other DB management Tool) to return the resulting Table as needed in the Database;

    You can then first try the same Query in our table.

    If our Table doesn't return it exactly as needed - our Plugin is not the same as a Database management Tool, it is running all our Queries through a PHP & SQL Parser.

    If that happens, you can prepare a MySQL view directly in your Database (which will return the data that you need, call it e.g. “view1” and then build a wpDataTables based on a simple query like "SELECT * FROM view1″.

     If you need help with that, you can see our video, where we show an example of using an SQL View in our plugin.

    -

    The data from Formidable Form is stored in multiple SQL Tables in the WP Database as serialized JSON.

    So if you manage to pull that data, you would still need to unserialize the JSON data to have it more 'humanly readable'.

    This falls under advanced/custom MySQL, and unfortunately, it will not be covered by our support.

    You will have to search for custom solutions online, for example, such as this one from Stack Overflow

    I hope it helps.


    Let me know if you try the first method, to simply filter the Table linked from Formidable Form by the user ID and if you encounter any issue with that use-case.

    Thank you.


    Kind Regards, 

    Miloš Jovanović
    [email protected]

    Rate my support

    Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/

    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