• I have a custom page template with html table.

    I wrote a PHP for numeric pagination (e.g. 1 2 3 4 … 9 10) in a html table. But the snippet is not working. I got only a blank table and no pagination.

    Here’s the snippet:

    $rows_per_page = 2;
        $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
    
        $pagination_args = array(
         'base' => @add_query_arg('paged','%#%'),
         'format' => '',
         'total' => ceil(sizeof($rows)/$rows_per_page),
         'current' => $current,
         'show_all' => false,
         'type' => 'plain',
        );
    
        if($wp_rewrite->using_permalinks())
         $pagination_args['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s',get_pagenum_link(1))) . 'page/%#%/', 'paged');
    
        if(!empty($wp_query->query_vars['s']))
         $pagination_args['add_args'] = array('s'=>get_query_var('s'));
    
        echo paginate_links($pagination_args);
    
        $start = ($current - 1) * $rows_per_page;
        $end = $start + $rows_per_page;
        $end = (sizeof($rows) < $end) ? sizeof($rows) : $end;
    
        if (isset($_POST['list_position']) && $_POST['list_position'] != 'Select by Position'){
        	$list_position= $_POST['list_position'];
        	$result_position= $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, process_resume, message, attachment_resume_id FROM resume_databank WHERE position= '" . $list_position . "' ORDER BY position ASC", OBJECT));
        		echo '<table>';
        		echo '<tr>';
        		$dir="";
        		$file="";
        		$optionId = 0;
        		echo '<th>Submit Time</th>';
        		echo '<th>Last Name</th>';
        		echo '<th>First Name</th>';
        		echo '<th>Middle Name</th>';
        		echo '<th>Mobile Number</th>';
        		echo '<th>Email</th>';
        		echo '<th>Location</th>';
        		echo '<th>Position</th>';
        		echo '<th>Message</th>';
        		echo '<th>Resume</th>';
        		echo '<th>Processed?</th>';
        	foreach ($result_position as $record_s){
        		$optionId++;
        		for ($i=$start;$i < $end ;++$i){
        			$record_s = $result_position[$i];
        		//$optionId = $record_s['id'];
        		echo '<tr>';
        		echo '<td id="submit_time">' . $record_s->submit_time . '</td>';
        		echo '<td id="last_name">' . $record_s->last_name . '</td>';
        		echo '<td id="first_name">' . $record_s->first_name . '</td>';
        		echo '<td id="middle_name">' . $record_s->middle_name . '</td>';
        		echo '<td id="mobile_number">' . $record_s->mobile_number . '</td>';
        		echo '<td id="email">' . $record_s->email . '</td>';
        		echo '<td id="location">' . $record_s->location . '</td>';
        		echo '<td id="position">' . $record_s->position . '</td>';
        		echo '<td id="message">' . $record_s->message . '</td>';
        		echo '<td id="resumeFile'.$optionId.'"><a href=' . wp_get_attachment_url($record_s->attachment_resume_id) . '>Download Resume</a></td>';
        		echo '<td id="radioOption><label for="Yes">Yes</label>
        				  <input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
        				  <label for="No">No</label>
        				  <input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption" value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
        		echo '</tr>';
        	} }
        		echo '</table>';
        }
Viewing 10 replies - 1 through 10 (of 10 total)
  • There are a few plugins already available but if you are really looking for custom pagination code this link here did the trick for me in the past.

    See if that is helpful.

    ~Tanveer

    Thread Starter UserDG

    (@tenkepadu)

    I have the update code, but when I clicked the link for the 2nd page I got a blank page

    if (isset($_POST['list_position']) && $_POST['list_position'] != 'Select by Position'){
    $list_position= $_POST['list_position'];
    $result_position= $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, message, attachment_resume_id FROM resume_databank WHERE position= '" . $list_position . "' ORDER BY position ASC", OBJECT));
    
    $rows_per_page = 3;
    $current = (intval(get_query_var('paged'))) ? intval(get_query_var('paged')) : 1;
    print $current;
    $pagination_args = array(
    'base' => @add_query_arg('paged','%#%'),
    'format' => '',
    'total' => ceil(sizeof($result_position)/$rows_per_page),
    'current' => $current,
    'show_all' => false,
    'type' => 'plain',
    );
    
    if( $wp_rewrite->using_permalinks())
    $pagination_args['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s',get_pagenum_link(1))) . 'page/%#%/', 'paged');
    
    if( !empty($wp_query->query_vars['s']))
    $pagination_args['add_args'] = array('s'=>get_query_var('s'));
    
    $start = ($current - 1) * $rows_per_page;
    $end = $start + $rows_per_page;
    $end = (sizeof($result_position) < $end) ? sizeof($result_position) : $end;
    
    echo '<table>';
    echo '<tr>';
    $optionId = 0;
    echo '<th>Submit Time</th>';
    echo '<th>Last Name</th>';
    echo '<th>First Name</th>';
    echo '<th>Middle Name</th>';
    echo '<th>Mobile Number</th>';
    echo '<th>Email</th>';
    echo '<th>Location</th>';
    echo '<th>Position</th>';
    echo '<th>Message</th>';
    echo '<th>Resume</th>';
    echo '<th>Processed?</th>';
    //foreach ($result_position as $record_s){
    for ($i=$start;$i < $end ;++$i ){
    $row = $result_position[$i];
    $optionId++;
    echo '<tr>';
    echo '<td id="submit_time">' . $row->submit_time . '</td>';
    echo '<td id="last_name">' . $row->last_name . '</td>';
    echo '<td id="first_name">' . $row->first_name . '</td>';
    echo '<td id="middle_name">' . $row->middle_name . '</td>';
    echo '<td id="mobile_number">' . $row->mobile_number . '</td>';
    echo '<td id="email">' . $row->email . '</td>';
    echo '<td id="location">' . $row->location . '</td>';
    echo '<td id="position">' . $row->position . '</td>';
    echo '<td id="message">' . $row->message . '</td>';
    echo '<td id="resumeFile'.$optionId.'">attachment_resume_id) . '>Download Resume</td>';
    echo '<td id="radioOption><label for="Yes">Yes</label>
    <input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
    <label for="No">No</label>
    <input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption" value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
    echo '</tr>';
    }
    echo '</table>';
    echo paginate_links($pagination_args);
    Thread Starter UserDG

    (@tenkepadu)

    I tried your suggestion but its not working

    Thread Starter UserDG

    (@tenkepadu)

    I tried another code, but still no luck. I get a blank page when I clicked the link for 2nd page

    Hi,

    Sorry to hear that. I don’t think I can debug your code as that requires a lot of set up at my end.

    If you can then try this:
    https://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin

    ~Tanveer

    Thread Starter UserDG

    (@tenkepadu)

    I tried that already but still not working. My update code is almost working except when clicked the link for 2nd page, it gives me blank

    Can you check the Chrome console to see if there are any obvious errors?

    If not that then may be you can debug your code using the debugger on WAMP localhost server to see what might be the issue?

    Thread Starter UserDG

    (@tenkepadu)

    There’s no error in the error log for that issue hmm

    Moderator keesiemeijer

    (@keesiemeijer)

    What does the query return?

    $result_position= $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, message, attachment_resume_id FROM resume_databank WHERE position= '" . $list_position . "' ORDER BY position ASC", OBJECT));

    Check it with this:

    echo '<pre>';
    print_r( $result_position );
    echo '</pre>';

    I’m guessing this doesn’t work

    'total' => ceil(sizeof($result_position)/$rows_per_page),

    Thread Starter UserDG

    (@tenkepadu)

    there’s a value in

    echo '<pre>';
    print_r( $result_position );
    echo '</pre>';

    can you help me to fix it? please

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to add a pagination properly using PHP?’ is closed to new replies.