Html表中的分页不起作用

时间:2016-05-24 作者:User014019

我有一个自定义页面模板,我发现了一个PHP代码段,可以在html表中创建分页。但代码工作不正常。

更新:我更改代码。但代码仍然不能正常工作,当我单击第二页时,第二页只显示一个空白页。

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;

global $wp_rewrite;

$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\'));
    echo paginate_links($pagination_args);

$start = ($current - 1) * $rows_per_page;
$end = $start + $rows_per_page;
$end = (sizeof($result_position) < $end) ? sizeof($result_position) : $end;

    echo \'<table id="paginate_result">\';
        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">\' . $result_position[0]->submit_time . \'</td>\';
            echo \'<td id="last_name">\' . $result_position[0]->last_name . \'</td>\';
            echo \'<td id="first_name">\' . $result_position[0]->first_name . \'</td>\';
            echo \'<td id="middle_name">\' . $result_position[0]->middle_name . \'</td>\';
            echo \'<td id="mobile_number">\' . $result_position[0]->mobile_number . \'</td>\';
            echo \'<td id="email">\' . $result_position[0]->email . \'</td>\';
            echo \'<td id="location">\' . $result_position[0]->location . \'</td>\';
            echo \'<td id="position">\' . $result_position[0]->position . \'</td>\';
            echo \'<td id="message">\' . $result_position[0]->message . \'</td>\';
            echo \'<td id="resumeFile\'.$optionId.\'"><a href=\' . wp_get_attachment_url($result_position[0]->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>\';

1 个回复
最合适的回答,由SO网友:Karthikeyani Srijish 整理而成

您应该分别获得total和result数组。Total用于创建页码,结果数组将为每个页面提供有限的数据。请参阅以下代码。

if (isset($_POST[\'list_position\']) && $_POST[\'list_position\'] != \'Select by Position\'){
    $list_position= $_POST[\'list_position\'];
    $totalPosition= $wpdb->get_var("SELECT count(id) FROM resume_databank WHERE position= \'" . $list_position . "\' ORDER BY position ASC");
$rows_per_page = 3;
$current = (intval(get_query_var(\'paged\'))) ? intval(get_query_var(\'paged\')) : 1;

global $wp_rewrite;

$pagination_args = array(
    \'base\' => @add_query_arg(\'paged\',\'%#%\'),
    \'format\' => \'\',
    \'total\' => ceil(sizeof($totalPosition)/$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 = ($totalPosition < $end) ? $totalPosition : $end;
$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 LIMIT $start,$end", OBJECT));

    echo \'<table id="paginate_result">\';
        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">\' . $result_position[0]->submit_time . \'</td>\';
            echo \'<td id="last_name">\' . $result_position[0]->last_name . \'</td>\';
            echo \'<td id="first_name">\' . $result_position[0]->first_name . \'</td>\';
            echo \'<td id="middle_name">\' . $result_position[0]->middle_name . \'</td>\';
            echo \'<td id="mobile_number">\' . $result_position[0]->mobile_number . \'</td>\';
            echo \'<td id="email">\' . $result_position[0]->email . \'</td>\';
            echo \'<td id="location">\' . $result_position[0]->location . \'</td>\';
            echo \'<td id="position">\' . $result_position[0]->position . \'</td>\';
            echo \'<td id="message">\' . $result_position[0]->message . \'</td>\';
            echo \'<td id="resumeFile\'.$optionId.\'"><a href=\' . wp_get_attachment_url($result_position[0]->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>\';

相关推荐

input data and output table

我需要一个解决方案,这样做与WP。在管理方面:我需要将数据输入数据库(名称、姓氏、证书、级别、发布日期)。在前端:客户端可以使用特定的搜索字段在插入的数据中进行搜索,并获得包含结果的表。(姓名、认证、级别、颁发日期)。任何模块都可以做到这一点。