0

Is it possible to create a numbered pagination of a table which is coming from custom created WordPress table? I want to show only 10 entries on a page then 10 on page number two and so on...

Below is my code

<?php
//Initializing variables to generate LIKE query.
$cr_table_array_keys = array_keys($cr_table_array_new);
            $index = 0;
            foreach($cr_table_array_new as $cr_table_index)
                {
                $cr_filter_variable[$index] = $_POST[$cr_table_array_keys[$index ]];
    $index ++;
}

//Generating LIKE query with multiple conditions.
$table_name = $wpdb->prefix. "$my_table_name";
$sql = "SELECT * FROM $table_name WHERE 1=1";
$params = [];
$cr_table_array_keys = array_keys($cr_table_array_new);
            $index = 0;
            $cr_id_skip = 1;
            foreach($cr_table_array_new as $cr_table_index)
                {
                if ( trim($cr_filter_variable[$index]) ) {
                $sql .= " AND ". $cr_table_array[$cr_id_skip]['data_field']." LIKE %s";
                    $params[] = $wpdb->esc_like($cr_filter_variable[$index]);
        }
    $index++;
$cr_id_skip++;
}
$results = $wpdb->get_results( $wpdb->prepare( $sql, $params ) );
//Will fire if no matching result were found.
if(!$results)
{
    echo "Oops, No record were found.";
}

//Fetching all table rows from database.
if( current_user_can('administrator')){?>
<div class="cr-custom-data-table">
<table>
    <tr>
        <?php
            $cr_table_array_keys = array_keys($cr_table_array_new);
            $index = 0;
            foreach($cr_table_array_new as $cr_table_index)
                {?>
                <th>
                <?php

                    print_r($cr_table_array_keys[$index ]);
                ?>
                </th>
        <?php
    $index ++;
}
    ?>
    </tr>
<?php

foreach ($results as $retrieved_data){ 
    echo '<tr>';
    foreach ($cr_table_array as $file ) {
    if($file['data_field'] != "id"){
    ?>
    <td>
        <?php 
            echo $retrieved_data-> $file['data_field']; 
        ?>
    </td>
<?php
    } 
}
    echo '</tr>';
}?> 
</table>
</div>
<?php }

?>

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.