如何使用PHP在html表中添加分页


How to add a pagination in a html table using PHP?

我有一个PHP代码片段,它在表行中显示了一个结果。我正在尝试在表格末尾用(1 2 3 4…10)进行分页。每页显示的最小行数仅为15。

但我的代码不起作用。请帮我

$results_per_page = 10;
$current = get_query_var('paged') ? get_query_var('paged') : 1;
$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, message FROM tablename 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>Position</th>';
        echo '<th>Message</th>';
        echo '<th>Resume</th>';
        echo '<th>Processed?</th>';
    foreach ($result_position as $record_s){
        $optionId++;
        //$optionId = $record_s['id'];
        for ($i=$start;$i < $end ;++$i ){
        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="position">' . $record_s->position . '</td>';
        echo '<td id="message">' . $record_s->message . '</td>';
        echo '<td id="resumeFile'.$id.'"><a href="' . $dir  . $file . '">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>';
}
如果您想尝试,jQuery分页是另一个选项吗?https://datatables.net/examples/basic_init/zero_configuration.html以下是您可以在php中轻松实现的datatables链接。