How to add a pagination properly using PHP?

I have a PHP that create a pagination in a html table. But the snippet is not working. I got only a blank table and no pagination.

I update the PHP snippet, but still not working, it displayed the duplicate of the data from the database instead of display a numeric pagination

Read More

How to fix this?

Snippet update:

<?php
$per_page = 2;

$row_count = $wpdb->get_var("SELECT COUNT(*) FROM resume_databank"); 
$total_results = $wpdb->get_var($row_count);
$total_pages = ceil($total_results / $per_page);

if (isset($_GET['page'])) {
$show_page = $_GET['page'];
    if ($show_page > 0 && $show_page <= $total_pages){
        $start = ($show_page - 1) * $per_page;
        $end = $start + $per_page;
    } else {
        $start = 0;
        $end = $per_page;
    }
} else {
    $start = 0;
    $end = $per_page;
}
    $page = intval($_GET['page']);
    $tpages=$total_pages;
    if ($page <= 0)
    $page = 1;

$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1){
    echo paginate($reload, $show_page, $total_pages);
}
    echo "</ul></div>";

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));
        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>';
    for ($i = $start; $i < $end; $i++){
        foreach ($result_position as $record_s){
            $optionId++;
            //$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>';
    }
}

function paginate($reload, $page, $tpages){
$adjacents = 2;
$prevlabel = "&lsaquo; Prev";
$nextlabel = "Next &rsaquo;";
$out = "";
// previous
if ($page == 1){
    $out.= "<span>".$prevlabel."</span>n";
} elseif ($page == 2){
    $out.="<li><a href="".$reload."">".$prevlabel."</a>n</li>";
} else {
    $out.="<li><a href="".$reload."&amp;page=".($page - 1)."">".$prevlabel."</a>n</li>";
}

$pmin=($page>$adjacents)?($page - $adjacents):1;
$pmax=($page<($tpages - $adjacents))?($page + $adjacents):$tpages;

for ($i = $pmin; $i <= $pmax; $i++){
    if ($i == $page) {
        $out.= "<li class="active"><a href=''>".$i."</a></li>n";
    } elseif ($i == 1) {
        $out.= "<li><a href="".$reload."">".$i."</a>n</li>";
    } else {
        $out.= "<li><a href="".$reload. "&amp;page=".$i."">".$i. "</a>n</li>";
    }
}

if ($page<($tpages - $adjacents)){
    $out.= "<a style='font-size:11px' href="" . $reload."&amp;page=".$tpages."">" .$tpages."</a>n";
}

if ($page < $tpages){
    $out.= "<li><a href="".$reload."&amp;page=".($page + 1)."">".$nextlabel."</a>n</li>";
} else {
    $out.= "<span style='font-size:11px'>".$nextlabel."</span>n";
}
    $out.= "";
    return $out;
}
?>

Related posts

Leave a Reply