Ask your WordPress questions! Pay money and get answers fast! (more info)

converting acf repeater into datatables WordPress

Hey,
I created some repeater fields with advanced custom fields. [pic1]
my all purpous is to manage each user his data by acf repeater, and then create a form to show it to the user on frontend.
basicly, i know how i want to do it.
i have on my site also wpdatatables. i just want to be able to fill datatables with the acf repeater fields on each user profile.

i will be glad to gave u more specific informations about each plugin just tell me what u need to solve it.

pls, if there any chance that it can be done asap because my manager is pressing on me!

Answers (2)

2015-11-18

Andrea P answers:

Hello,

I read this tutorial and it seems that the only way would be to manually create a table for each user, but this table can be automatically generated and populated with some php.

http://wpdatatables.com/documentation/creating-wpdatatables/creating-wpdatatables-from-serialized-php-array/

I think the way to go would be creating a template file where to put the function which will grab the user-ID passed by the url, and output the string with the data for the table.
so the file will be one for all users, but then when you create the table for each user, you will select to populate it by passing a specific url like

http://mywebsite.com/user_data_table.php?user_id=12

so the file user_data_table.php would look something like this


<?php
// Initializing the WordPress engine in a stanalone PHP file
include('../wp-blog-header.php');

/** FUNCTION TO GET USER DATA AND PRINT A SERIALIZED ARRAY **
*/
function display_user_data_serialized($user_id){
// quit if no user
if ( !$user_id ) return;

$return_array = array(); // Initializing the array that will be used for the table

// get user repeater field and loop the rows

if ( have_rows('repeater_name', 'user_'.$user_id) ):
while ( have_rows('repeater_name', 'user_'.$user_id) ) : the_row();

// populate the return array with the data from the current row
$return_array[] = array(
'field 1 label' => get_sub_field('field_1_slug'),
'field 2' => get_sub_field('field_2_slug'),
'field 3' => get_sub_field('field_3_slug'),
);

endwhile;
endif;

// Now the array is prepared, we just need to serialize and output it
echo serialize( $return_array );
}


// get the user id
$user_id = $_GET['user_id'];
// call our function
display_user_data_serialized($user_id);

?>


note that you have to edit the function and grab/add your repeater subfileds where I put those placeholders names

after creating this file you can go to step 2 of the tutorial and create a new table for each user, pasting the correct url as a source, which points to the previously created file, but also passes the consistent user id in the url.

then you can print the shortcode in the frontend page where you want to display the data from the user.

I hope this would help!


heiko comments:

First of all tnx so much.
I appriciate your way to do that but I think ahead and because the need for filtering and extra funcion that wpdatatables provide.
Mabey I don't explain myself right.
For each user I have a page account.
In this page they have some repeaters that contain their data.
My purpous is not to create a datatable for each user.
I want to create compatible wpdatatables for each repeater and after user logged in I want to fill that tbl with the user info from the back end repeater.
So for example if we dill with the repeater operations:
Columns: operation_date, debit, credit.
I want to be able to determine wpdatatables with ecsactly same columns. Put its short code in the relevant page and when the user navigate to that page he can see his own operations.


heiko comments:

First of all tnx so much.
I appriciate your way to do that but I think ahead and because the need for filtering and extra funcion that wpdatatables provide.
Mabey I don't explain myself right.
For each user I have a page account.
In this page they have some repeaters that contain their data.
My purpous is not to create a datatable for each user.
I want to create compatible wpdatatables for each repeater and after user logged in I want to fill that tbl with the user info from the back end repeater.
So for example if we dill with the repeater operations:
Columns: operation_date, debit, credit.
I want to be able to determine wpdatatables with ecsactly same columns. Put its short code in the relevant page and when the user navigate to that page he can see his own operations.


Andrea P comments:

Hello,

have you followed the tutorial which I linked?

I mean that the code which I pasted is just one of the steps of the tutorial, you must follow the rest and it should be what you asked..
http://wpdatatables.com/documentation/creating-wpdatatables/creating-wpdatatables-from-serialized-php-array/

so:

STEP 1
you create a file with that code which I provided. but you must tweak this bit and add the labels and slugs of the fields on the repeater:
// populate the return array with the data from the current row

$return_array[] = array(

'field 1 label' => get_sub_field('field_1_slug'),

'field 2' => get_sub_field('field_2_slug'),

'field 3' => get_sub_field('field_3_slug'),

);


then that page will output nothing, if in the url you don't pass a valid user id as well ( ?user_id=12 ).
if you pass the id, it will print out a string which is a serialized array that can be used to populate a wpdatatable


STEP 2
then you create a new table in wpdatatable (for each user), and you select to pupulate it from a php array.
as source for the table, you place an url which points to the file created in step1, but also adding the id of the user which you are creating the table for. by instance:

http://mywebsite.com/name_of_the_file.php?user_id=12


STEP 3 (actually step4 in the tutorial, because step 3 is optional)
now you can paste the shortcode of this table, into the profile of the user with the ID that you have added to the url.
the table should pick up the values from the repeater, as the source file is providing a string with those fields in it.


if you follow this procedure and the table is empty, it is likely that there is some error in the repeater loop.. so maybe you can post the code that you are using?

cheers


heiko comments:

u dont understand my porpous!
i dont want to create wpdatatable for each user. its unneccery because acf doing it for me.
the users profile data manage on backend by filed group that i created containing thos repeaters i want to export to wpdatatable.

i want to create wpdatatable for each repeater i have on backend user profile, past its shortcode on UserAccount page, so the datatable dosent change to each user, its have a static headers but dynamic data. then by some manipulation after each repeater have a suite wpdatatable i want to load the current loggedin user data.

* for me, each user redirect to the same account page with the same wpdatatableid.
the only thing that i want to be diffrent is the data filled in the table!


heiko comments:

maybe i wasnt clear!
my users profile managed by advanced cusom fields.
for exampe: in the backend user profile i have a repeater named "operations".
whan i add new user this repeater empty. i manuel fill it with relevant data for each user.
the repeater headers are the same for all the users.

then i create a a page for user account on frontend.
i would like to paste this page a shortcode for wpdatatable that binding with the repeater on backend.
your solution is to create new table for each user, and that dosent what i try to do!


Andrea P comments:

Yes, you're probably right, I don't understand your purpose, I'm sorry but it is not very clear indeed..

let try to recap.

do you have a repeater field in each user profile. correct?

then:
1) do you want to display a single wpdatatable which contains all the repeater from all the users?
2) do you want to display a table on each user profile, which display data from only one user?

if you want the option 2), then that's what I am suggesting to do.

the only way to display a wpdatatable on frontend, seems to be by creating it in the backend. it can't be dynamically created directly from frontend (at least I haven't found documentation about this). though, after is created, it can be dynamically populated by a php string/file .

so the only way to go, would be to manually create a wpdatatable for each user, but instead of populating it from backend, you set it to be populated dynamically by a php string (which is the one created by the file above).


in any case, if you want you could still use one single page to display the table, and code it so that the people would see their own table when going there.

in order to do so, you need to set up another field for the users (which should be not editable/visible by them), and place in that field the ID of the wpdatatable which is connected to their profile data.

then you could paste something like this in a template file for the page where you want to display the table:

<?php
//go on only if user is logged
if ( is_user_logged_in() ){
// grab the current user id
$current_user_id = get_current_user_id();

// grab the value of their table ID (put here the slug of the field where you stored the table id)
$table_id = get_field( 'user_table_id', 'user_'.$current_user_id );

// display the table
echo do_shortcode('[wpdatatable id='.$table_id.']');

}
?>


the above code will display a different table shortcode for each user viewing the page, and it will grab the id of the wpdatatable from a custom field in the user profiles (which shouldn't be visible/editable by users, and filled by an admin when he set up the actual table)


heiko comments:

i so appriciate your pacient! tnx again.
off course that i would create those wpdatatables in backend. i want to do it by serialized php array.
so i create OperationsTable and choose to serielize it by php array - then i take the shortcode of this table and paste it to the OpertaionsPage.
those wpdatatables ids sould be constant.
each page sould contain the shortcode wpdatatable id wich comptibale with the repeater save this data on backend.

i know that it can be done because i saw this kind of implement, just cant find the way to do that.

to be more clear:

on user custom field i have repeater named operations with thos headers:
'Date opération'
'Date de valeur'
'Libellé'
'Débit'
'Crédit'

this repeater is an acf field. and it generate this data in every user backend account page.

then i have a login system that redirect each user to AccountPage.
on this AccountPage i want to past shortcode connect to wpdatatableid. this shortcode dosent change for each user just like the repeater containing each user's data only created once.
then, every time user ridirecting to the AccountPage, the wpdatatable load his own data from the operations repeater on backend.
this way i dont need to create wpdatatable everytime i create a new user - its not good to me. the people i build the site for wont be able to go throw this every time.
if i can binding each repeater i have on backend, to wpdatatable on backend, just the filling php function will load the data that relevant to the current user loggedin.

Thanks in advanced!


heiko comments:

again, i think u dont get to the end of my opinion.
i dont want to create wpdatatable for each user i registred!
i want to create wpdatatable for each repeater i have on backend user profile!


heiko comments:

i gave the operations repeater as example.
once i would be able to do that i will find the way to do it with all the repeaters fields


Andrea P comments:

Hello,
so now it's clear, thanks! :)

the last thing I've suggested is allowing you to go very close to what you need.

you would paste the last bit of code into your AccountPage, and then create a new field for users, in which you can store the ID of the table created for that specific user.
when a user visits AccountPage, the shortcode will be dynamically built and will re-call the wpdatatable for that user.

I understand that you'd like to have a code which create a new wpdatatable automatically, but it doesn't seem to be a supported feature of the plugin. you can populate the table dynamically, but they don't have documentation about a php function to create the table directly with php.

they surely have a class and some php functions to create the table within their core functions, but I'd have to dig into the plugin code, find out how they do it, and then use the same functions to create and set up a table (populated via php array) each time you update your users (if the table doesn't exists already). and also add the newly created table id to the user's custom field.

I'm not even sure that this is feasible, but it would definitely require quite a bit of effort and a bigger budget, I am sorry.
and I will need to see the plugin files too, as it's not a free plugin and I haven't got a copy of it..


Andrea P comments:

wait a second.
maybe I was not understanding.

do you have more than one repeater for each user?
and you want to create one table for each user but populate it with data from all his repeaters together?


heiko comments:

i'm so sorry. i think u still dont get me!
i dont want to create any wpdatatable automaticly!

Lets try this:

each user has operations(repeater) and vierment(also repeater).
i create two wpdatatables populating from phparray.
the first wpdatatable's (operationsDT) shortcode paste in page - OperaionsPage.
the second wpdatatable's (viermentsDT) shortcode paste in page - ViermentsPage.

lets say i have user A.
this user has data on those repeaters fields that was manual hendley from backend.
when user A log in he redirect to OperationsPage.
this page allready has it wpdatatableid.
the only thing that left is to recognize the current user loggedin and fill it with the repeater field from backend.

i know it can be done!

if the budget to low let me know! i must to find a way to do that! pls help me!


heiko comments:

Yes - I have more than one repeater for each user.
but NO- i dont want to create datatable for each user! i want to create datatable for each repeater and manipulate the data so it will be relevant to the current user!


heiko comments:

i dont want to create one table for each user but populate it with data from all his repeaters together
i want that every datatable will be responsive for each repeater field i have.


Andrea P comments:

Hello Heiko,

I see what you mean but I think it is not feasible. at least not with wpdatatable.

when you create the table (i.e. table_for_repeater_1), you have to specify an url from where the table will get the serialized php array to populate its content.

unfortunately, when the plugin is going to check that url and get the string, it won't be an actual user visiting that url, hence we won't have a user_id to pick the acf fields from.
so there is no way to build up the string on fly based on the current user viewing the account page, because when you display the table with the shortcode in the AccountPage, the table is actually already populated in the backend and it picks the string from the php file, which is not connected with the current user viewing the account page..

that's why you must have a table for each user (and each repeater), with a specific link which passes the user id to the php file, which then will output an array string that is specific for that user.


the point now is: why do you actually need to use wpdatable to display those tables to the users frontend? which are the table functionalities that you must have on frontend?
can't you build up the tables in frontend simply with html? or maybe using a jquery plugin like the one which is at the base of wpdatatables?
http://www.datatables.net/

then you won't even need to create a table in wpdatable, and rather place some code in AccountPage which will print out the html for the table, based on the current_user_id and the specific repeater that you want to display. and then eventually apply the jquery features in order to display it nicely.

basically this would be what Rempty suggested, but the $user_id would be get_current_user_id()

I'm sorry but the fact you wanted to use wpdatatable (and that your needs were more complex than what I understood at first), have pushed me to the wrong path. the only way to achieve what you want is to manually build up the table by using html and a custom call for jQuery datatables.


heiko comments:

sorry i lost you.
u can help me with this kind of solution?
i really dont care how to display thos repeaters.
i choose in wpdatatables because thier have a custom filter on datatables wich would save me time.
but if it cant be done by wpdatatables i will be happy to hear your solution.
can u help me figure out the code i'm supposed to write?
how to retrive the serialize data that acf save?
there is any SQL query to get thos subfields?
i lost my mine with this subject! im going crazy!


heiko comments:

my problem is only that my users profile allready created by acf so i dont care wich way to show it on frontend, just want to be able to custom it before the preview.
my only need right now is to get thos repeater fields into tables - html/ wp/ whatever u want!


Andrea P comments:

I thought you needed to have those tables in the backend as actual wpdatatables, which were populated by the repeater field, but were also available as wpdatatable in the backend, with all the functionalities connected to it.

if you don't want to have a backend wpdatatable, and you just want to display the repeaters in a table style, there is nothing tricky about it, it is just about looping the repeater field grabbed from the $current_user, and build up some html to display the fields.
and the solution is basically what Rempty suggested in his first post.

you basically have to create a template-file for each of the pages where you want to display the table, and tweak the function in order to display the headings of the relevant repeater, and to grab the field names of the relevant repeater.

the function will build and output the html for the table, populated dynamically with the data from the repeater of the given user

then you use this code to call the function and to instantiate the jquery which will render the table in a nice looking way.


<?php
//Render the table
echo create_datatable( get_current_user_id() );
?>

<script type="text/javascript">
//Initialize the datatable jQuery
jQuery(document).ready(function() {
jQuery('#datatableuser').DataTable();
} );
</script>


both the function and the call, have to be placed in the template file.
so you'll have like

template-repeater1.php
template-repeater2.php

do you understand what I am talking about?


heiko comments:

OK.
yes i start to realize waht to do,
i implement all your solution but it gave me nothing

this is my operations.php:

<?php

/*

Template Name: profiletest

*/
get_header();
?>



<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

<?php

function create_datatable($userid){

$table='';

if ( have_rows('operations', 'user_'.$userid) ):

$table='<table id="datatableuser" width="100%" cellspacing="0">

<thead>

<tr>

<th>Date opération</th>

<th>Date de valeur</th>

<th>Libellé</th>

<th>Débit</th>

<th>Crédit</th>

</tr>

</thead>

<tfoot>

<tr>

<th>Date opération</th>

<th>Date de valeur</th>

<th>Libellé</th>

<th>Débit</th>

<th>Crédit</th>

</tr>

</tfoot>

<tbody>

';

while ( have_rows('operations', 'user_'.$userid) ) : the_row();

$table.='<tr>

<td>'.get_sub_field('date_operation').'</td>

<td>'.get_sub_field('date_de_valeur').'</td>

<td>'.get_sub_field('libelle').'</td>

<td>'.get_sub_field('debit').'</td>

<td>'.get_sub_field('credit').'</td>

</tr>';

endwhile;

$table.='</tbody>

</table>

';

endif;

return $table;

}

?>


//Render the table
$user_id = get_current_user_id();
echo create_datatable($userid);



And this javascript to initialize the datatable.

<script type="text/javascript">

//Initialize the datatable jQuery

jQuery(document).ready(function() {

jQuery('#datatableuser').DataTable();

} );

</script>



</main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>


as u can see for while i am using a test template.
i dont get any results - the content zone of the page is empty!


Andrea P comments:

I have created a function which can be used for all the repeaters, and then when you call it, you have to pass the values of which repeater you want to display.
I think this would be easier to implement and to maintain. ;)


/** RENDER A TABLE FROM A SPECIFIC REPEATER FIELD
*
* @param $user_id {int} (requried) the id of the user to retrieve the data from
* @param $repeater_name {string} (required) the slug of the repeater field
* @param $headings {ordered array} an array of sub-fields, set up with the KEY as the field slug, and the VALUE as the field label. i.e. array('sub_field_1'=>'Sub Field Label', 'sub_field_2'=>'Another Label')
*
*/

function AP_create_datatable($userid, $repeater_name, $headings){
if ( !$userid || !$repeater_name || !$headings ){
return;
}
$table='';
if ( have_rows($repeater_name, 'user_'.$userid) ):

$table .='<table id="datatableuser" width="100%" cellspacing="0"><thead><tr>';
// add the headings
foreach ($headings as $slug => $label){
$table .= '<th>'.$label.'</th>';
}

$table .= '</thead></tr>';

// add the footer headings
$table .= '<tfoot><tr>';
foreach ($headings as $slug => $label){
$table .= '<th>'.$label.'</th>';
}
$table .= '</tr></tfoot>';

// print the table rows
$table .= '<tbody>';

while ( have_rows($repeater_name, 'user_'.$userid) ) : the_row();

$table .= '<tr>';
// loop each field name, get the value and print it out
foreach ($headings as $slug => $label){
$table .= '<td>'.get_sub_field($slug).'</td>';
}

$table .= '</tr>';

endwhile;

$table.='</tbody></table>';

endif;

return $table;

}



so you can place that code in functions.php

then in each template-page file, you just call it with this code:


<?php
/** EXAMPLE OF THE FUNCTION CALL
* ADD THE RELEVANT REPEATER NAME AND SUBFIELDS ARRAY
*
*/

//Render the table
echo AP_create_datatable( get_current_user_id(), 'repeater_1_slug', array('sub_field_1'=>'Sub Field Label', 'sub_field_2'=>'Another Label') );
?>

<script type="text/javascript">
//Initialize the datatable jQuery
jQuery(document).ready(function() {
jQuery('#datatableuser').DataTable();
} );

</script>


as you can see, the function has some parameters, so you can pass the repeater slug, and all the subfields which you want to get and display. the subfields are placed in an array, with KEY as the actual slug, and VALUE as the label that you want to use in the table headings


Andrea P comments:

and obviously, as that code will pull the data from the current user, means that if you are not logged, you won't see anything, and if you are logged in with your account, you will see your own repeater data (if you have any...).

for test purposes, you can change the call and specify a user id which you know it has some data.
so in the function call within the template, you would substitute get_current_user_id() with the actual user that you want to use for testing


heiko comments:

OK, Wow.
first of all, thank u! because of people like u i still belive in the world.
you actually spent all day trying to help me and its priceless for me, so i want to thank u so much and if there anything i can do for halping u (althoug i dont think u ever gona need) pls let me know!

i did exactly what u told me.
i hava an page call profileTest with the profiletest template i created:


<?php

/*

Template Name: profiletest

*/
get_header();
?>



<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">



<?php

/** EXAMPLE OF THE FUNCTION CALL

* ADD THE RELEVANT REPEATER NAME AND SUBFIELDS ARRAY

*

*/



//Render the table

echo AP_create_datatable( 2, 'operations', array('Date opération'=>'date_operation', 'Date de valeur'=>'date_de_valeur', 'Libellé'=>'libelle', 'Débit'=>'debit', 'Crédit'=>'credit') );

?>



<script type="text/javascript">

//Initialize the datatable jQuery

jQuery(document).ready(function() {

jQuery('#datatableuser').DataTable();

} );



</script>


</main><!-- #main -->
</div><!-- #primary -->

<?php get_footer(); ?>


i also past your function into function.php.

i do put the test id (2) of user that i know for sure that he has rows on that repeater,
the result is that the page stuck on the loading widget i have.

u hava any ideas why?


heiko comments:

do u know or work with acf repeater? or acf at all?
i ask it because every repeater belong to one and only fields group.
maybe we need to named it some where?


heiko comments:

also, i have been thinking- 0
maybe the problem is with the repeater subfields..
the first 2 headers are Date type. is there any problem with that?


heiko comments:

has i expected i think there are some problem with the jquery library because when i search in the DOM for element with the table id, i do get this:


<table id="datatableuser" width="100%" cellspacing="0">
<thead>
<tr>
<th>Date opération</th>
<th>Date de valeur</th>
<th>Libellé</th>
<th>Débit</th>
<th>Crédit</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Date opération</th>
<th>Date de valeur</th>
<th>Libellé</th>
<th>Débit</th>
<th>Crédit</th>
</tr>
</tfoot>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td>8</td>
<td>5</td>
</tr>
<tr>
<td></td>
<td></td>
<td>KKKKK</td>
<td>4</td>
<td>9</td>
</tr>
</tbody>
</table>


as u can see all the data is arrived except the first two <td>is tags wich are date dields.
any suggestions?


heiko comments:

i just note that i have an error:
profiletest:198 Uncaught TypeError: jQuery(...).DataTable is not a function
so the table exist on the html document but it wont draw!


Andrea P comments:

Hello!

Thanks for your kind words! :)
actually I did also a lot of work on other things today, even though I've followed your issue as well. ;)

this kind of things actually are a god learning and testing exercise for me, even though I tend to dedicate time only on this site because at least I can get some kind of donation-like reward.

anyway, I see 2 errors here.

one it's easy to correct. you have to invert the labels and slugs in the fields array when you call the function.
you have put the label as Key and the slug as value, but it must be the opposite.
I am not sure why the html seems printing the labels/headings correctly though.. but it is clear that it is not pulling the fields correctly then, because the cells are empty (it worked only for the ones in which the label is the same as the slug -without the accents-).

the other error is that it seems not recognizing the jquery DataTable..

it might be that the wpdatatable plugin uses a personalized version of that jquery plugin with a different name? I'm sorry but I don't know..

could you try the code without the jquery script and see if at least it prints the data correctly?


heiko comments:

yes it does! (but the first two columns (date type) are still empty)
sorry.. i had notice the first error and change it alreadey but forgot to mantion so thats why its prenting the headers correctly.
what dose it matther how it use on wpdatatables? the all idea dosent was to avoid using it?


Andrea P comments:

it matters because I think that wpdatatable was loading the jquery plugin called DataTable, and then use it to display his table on frontend.
but maybe it is loading the plugin only on pages where it sees its own shortcode.

this means that you'll have to load the jquery plugin manually.

try adding this code to your functions.php


// ADD SCRIPTS and styles
function add_theme_custom_scripts(){
// load jquery dataTable styles
wp_register_style('datatable-style', '//cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css', false, NULL, false);
wp_enqueue_style('datatable-style');
// load scripts for dataTable
wp_register_script('jq-datatable-scripts', '//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js', array( 'jquery' ), NULL, false);
wp_enqueue_script('jq-datatable-scripts');

}
add_action( 'wp_enqueue_scripts','add_theme_custom_scripts');


p.s. it is very weird that it doesn't print out the first 2 fields.. are you sure that the slug of the field is exactly spelled as you wrote? are those fields actually filled with something for the test user? cause it seems that in the first row is actually missing the first 3 fields, and in the second row it misses only the first 2..

2015-11-18

Rempty answers:

If you can use the jQuery datatable https://www.datatables.net/ and do it manually, include the js and css needed
How to https://www.datatables.net/manual/installation

Now need a function to create the table

<?php
function create_datatable($userid){
$table='';
if ( have_rows('repeater_name', 'user_'.$userid) ):
$table='<table id="datatableuser" width="100%" cellspacing="0">
<thead>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
<th>Field 4</th>
<th>Field 5</th>
<th>Field 6</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
<th>Field 4</th>
<th>Field 5</th>
<th>Field 6</th>
</tr>
</tfoot>
<tbody>
';
while ( have_rows('repeater_name', 'user_'.$userid) ) : the_row();
$table.='<tr>
<td>'.get_sub_field('field_1').'</td>
<td>'.get_sub_field('field_2').'</td>
<td>'.get_sub_field('field_3').'</td>
<td>'.get_sub_field('field_4').'</td>
<td>'.get_sub_field('field_5').'</td>
<td>'.get_sub_field('field_6').'</td>
</tr>';
endwhile;
$table.='</tbody>
</table>
';
endif;
return $table;
}


You can echo the function where you want to appear

//Render the table
echo create_datatable($userid);
?>
And this javascript to initialize the datatable.
<script type="text/javascript">
//Initialize the datatable jQuery
jQuery(document).ready(function() {
jQuery('#datatableuser').DataTable();
} );
</script>


heiko comments:

First of all tnx so much.
I appriciate your way to do that but I think ahead and because the need for filtering and extra funcion that wpdatatables provide.
Mabey I don't explain myself right.
For each user I have a page account.
In this page they have some repeaters that contain their data.
My purpous is not to create a datatable for each user.
I want to create compatible wpdatatables for each repeater and after user logged in I want to fill that tbl with the user info from the back end repeater.
So for example if we dill with the repeater operations:
Columns: operation_date, debit, credit.
I want to be able to determine wpdatatables with ecsactly same columns. Put its short code in the relevant page and when the user navigate to that page he can see his own operations.


Rempty comments:

I maybe found a solution, but take some work.
Create a datatable from db querys.
And using variables you can pass the user id like this http://wpdatatables.com/documentation/table-features/using-placeholders/

http://wpdatatables.com/video-course/creating-new-wpdatatables-with-table-constructor-wizard-video/geenerating-wordpress-db-queries-with-table-constructor/


heiko comments:

i think this is what i was searching for!
the only thing that i cant be able to do is to query this repeater field.
its serialize and the meta_value is something like:

s:8:"order_no";i:2;}i:3;a:16:{s:3:"key";s:19:"field_5621380b40aee";s:5:"label";s:6:"Débit";s:4:"name";s:5:"debit";s:4:"type";s:6:"number";s:12:"instructions";s:0:"";s:8:"required";s:1:"0";s:12:"column_width";s:0:"";s:13:"default_value";s:1:"0";s:11:"placeholder";s:0:"";s:7:"prepend";s:0:"";s:6:"append";s:0:"";s:3:"min";s:0:"";s:3:"max";s:0:"";s:4:"step";s:0:"";s:17:"conditional_logic";a:3:{s:6:"status";s:1:"0";s:5:"rules";a:1:{i:0;a:2:{s:5:"field";s:4:"null";s:8:"operator";s:2:"==";}}s:8:"allorany";s:3:"all";}s:8:"order_no";i:3;}i:4;a:16:{s:3:"key";s:19:"field_5621382540aef";s:5:"label";s:7:"Crédit";s:4:"name";s:6:"credit";s:4:"type";s:6:"number";s:12:"instructions";s:0:"";s:8:"required";s:1:"0";s:12:"column_width";s:0:"";s:13:"default_value";s:1:"0";s:11:"placeholder";s:0:"";s:7:"prepend";s:0:"";s:6:"append";s:0:"";s:3:"min";s:0:"";s:3:"max";s:0:"";s:4:"step";s:0:"";s:17:"conditional_logic";a:3:{s:6:"status";s:1:"0";s:5:"rules";a:1:{i:0;a:2:{s:5:"field";s:4:"null";s:8:"operator";s:2:"==";}}s:8:"allorany";s:3:"all";}s:8:"order_no";i:4;}}s:7:"row_min";s:0:"";s:9:"row_limit";s:0:"";s:6:"layout";s:5:"table";s:12:"button_label";s:7:"Add Row";s:17:"conditional_logic";a:3:{s:6:"status";s:1:"0";s:5:"rules";a:1:{i:0;a:2:{s:5:"field";s:4:"null";s:8:"operator";s:2:"==";}}s:8:"allorany";s:3:"all";}s:8:"order_no";i:2;}

so i dont know how to get thos fields


Rempty comments:

Uhm, thats a big problem, acf save the values as serialize data. MySQL cant unserialize by it self.

WpDatatables have a lot of restrictions.

I think using wpdatatables is not the correct solution, and create your custom table is a better and easy solution.


heiko comments:

i dont mine dont use wpdatatables..
i cant fint other way.
all i need is some way to retrive the data from the repeater on backend to tables on frontend!


Rempty comments:

Use my solution, use jQuery Datatables (same as wpdatatables)
https://www.datatables.net/ and do it manually,
include the js and css needed How to https://www.datatables.net/manual/installation

Just need the names from the fields

<?php
function create_datatable($userid){
$table='';
if ( have_rows('repeater_name', 'user_'.$userid) ):
$table='<table id="datatableuser" width="100%" cellspacing="0">
<thead>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
<th>Field 4</th>
<th>Field 5</th>
<th>Field 6</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
<th>Field 4</th>
<th>Field 5</th>
<th>Field 6</th>
</tr>
</tfoot>
<tbody>
';
while ( have_rows('repeater_name', 'user_'.$userid) ) : the_row();
$table.='<tr>
<td>'.get_sub_field('field_1').'</td>
<td>'.get_sub_field('field_2').'</td>
<td>'.get_sub_field('field_3').'</td>
<td>'.get_sub_field('field_4').'</td>
<td>'.get_sub_field('field_5').'</td>
<td>'.get_sub_field('field_6').'</td>
</tr>';
endwhile;
$table.='</tbody>
</table>
';
endif;
return $table;
}

//Render the table
$userid = get_current_user_id();
echo create_datatable($userid);
?>


And this javascript to initialize the datatable.

<script type="text/javascript">
//Initialize the datatable jQuery
jQuery(document).ready(function() {
jQuery('#datatableuser').DataTable();
} );
</script>


Rempty comments:

Are you logged? You must be logged,



//the problem is wrong variable name
//replace
echo create_datatable($userid);
with
echo create_datatable($user_id);


Rempty comments:

In the Andrea's function is calling ACF functions to get the values, and must be working.
What error are u getting?

Maybe can give us the url of the page and help you.


heiko comments:

http://cptbusiness.com/profiletest


heiko comments:

http://cptbusiness.com/profiletest
thank u so much!!