如何使用PHP正确添加分页?

时间:2016-05-23 02:25:08

标签: php wordpress pagination html-table custom-wordpress-pages

我有一个PHP在html表中创建分页。但该片段无效。我只有一张空白的桌子,没有分页。

我更新了PHP代码段,但仍然无法正常工作,它显示了数据库中数据的副本,而不是显示数字分页

如何解决这个问题?

代码段更新:

<?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;
}
?>

0 个答案:

没有答案