1

目前我有 2 个表并且数据是动态的。

我需要分别为两个表添加分页。我已经使用分页器类添加了分页器,它工作正常。但问题是当我单击第一个表的下一步按钮时,两个表的内容都会更改为下一页。

分页器.php

<?php

// Paginator Class
// error_reporting(E_ALL); 

define("QS_VAR", "page"); // the variable name inside the query string (don't use this name inside other links)
define("STR_FWD", "Next&gt;&gt;"); // the string is used for a link (step forward)
define("STR_BWD", "&lt;&lt;Prev"); // the string is used for a link (step backward)

$scriptname = (isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '');
define("SCRIPT_NAME", $scriptname);
$v = (isset($_REQUEST['page_num_rows']) ? (is_numeric($_REQUEST['page_num_rows']) ? $_REQUEST['page_num_rows'] : 5) : 5);
define("NUM_ROWS", $v); // the number of records on each page

class Paginator {

    var $sql;
    var $result;
    var $get_var = QS_VAR;
    var $rows_on_page = NUM_ROWS;
    var $str_forward = STR_FWD;
    var $str_backward = STR_BWD;  
    var $all_rows;
    var $num_rows;
    var $page;
    var $number_pages;
    var $url_name = SCRIPT_NAME;

    // constructor
    function Paginator() {

    }

    // sets the current page number
    function set_page() {
        $this->page = (isset($_REQUEST[$this->get_var]) && $_REQUEST[$this->get_var] != "") ? $_REQUEST[$this->get_var] : 0;
        return $this->page;
    }

    // gets the total number of records
    function get_total_rows() {
        $tmp_result = mysql_query($this->sql);
        $this->all_rows = mysql_num_rows($tmp_result);
        mysql_free_result($tmp_result);
        return $this->all_rows;
    }

    // get the totale number of result pages
    function get_num_pages() {
        $this->number_pages = ceil($this->get_total_rows() / $this->rows_on_page);
        return $this->number_pages;
    }

    // returns the records for the current page
    function get_page_result() {
        $start = $this->set_page() * $this->rows_on_page;
        $page_sql = sprintf("%s LIMIT %s, %s", $this->sql, $start, $this->rows_on_page);
        $this->result = mysql_query($page_sql);
        return $this->result;
    }

    // get the number of rows on the current page
    function get_page_num_rows() {
        $this->num_rows = @mysql_num_rows($this->result);
        return $this->num_rows;
    }

    // free the database result
    function free_page_result() {
        @mysql_free_result($this->result);
    }

    function display_row_count() {
        $var = $this->get_var;
        $url_part1 = $this->url_name . "?";
        $url_part2 = "&" . $var . "=0" . $this->rebuild_qs($var);
        $select = "&nbsp;&nbsp;Show ";
        $select.="<form method=get name=page_num_rows_form action=\"$this->url_name\" >"; // [form used for javascript disabled case] -not working
        $select.="<select name=page_num_rows id=page_num_rows onChange=\"window.location='$url_part1'+'page_num_rows='+this.value+'$url_part2'\" >";
        $select.="<option value=50 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 50 ? ' selected ' : '') : '') . " >50</option>";
        $select.="<option value=100 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 100 ? ' selected ' : '') : '') . " >100</option>";
        $select.="<option value=150 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 150 ? ' selected ' : '') : '') . " >150</option>";
        $select.="<option value=200 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 200 ? ' selected ' : '') : '') . " >200</option>";
        $select.="<option value=500 " . (isset($_REQUEST['page_num_rows']) ? ($_REQUEST['page_num_rows'] == 500 ? ' selected ' : '') : '') . " >500</option>";
        $select.="</select>";
        $select.="<noscript>&nbsp;<input type=submit value=Go /></noscript>";
        $select.="</form>";  // form used for javascript disabled case -- not working
        $select.=" per page";
        return $select;
    }

    // function to handle other querystring than the page variable
    function rebuild_qs($curr_var) {
        if (!empty($_SERVER['QUERY_STRING'])) {
            $parts = explode("&", $_SERVER['QUERY_STRING']);
            $newParts = array();
            foreach ($parts as $val) {
                if (stristr($val, $curr_var) == false) {
                    array_push($newParts, $val);
                }
            }
            if (count($newParts) != 0) {
                $qs = "&" . implode("&", $newParts);
            } else {
                return false;
            }
            return $qs; // this is your new created query string
        } else {
            return false;
        }
    }

    // this method will return the navigation links for the conplete recordset
    function navigation($separator = " | ", $css_current = "", $back_forward = false) {
        $max_links = NUM_LINKS;
        $curr_pages = $this->set_page();
        $all_pages = $this->get_num_pages() - 1;
        $var = $this->get_var;
        $navi_string = "";
        if (!$back_forward) {
            $max_links = ($max_links < 2) ? 2 : $max_links;
        }
        if ($curr_pages <= $all_pages && $curr_pages >= 0) {
            if ($curr_pages > ceil($max_links / 2)) {
                $start = ($curr_pages - ceil($max_links / 2) > 0) ? $curr_pages - ceil($max_links / 2) : 1;
                $end = $curr_pages + ceil($max_links / 2);
                if ($end >= $all_pages) {
                    $end = $all_pages + 1;
                    $start = ($all_pages - ($max_links - 1) > 0) ? $all_pages - ($max_links - 1) : 1;
                }
            } else {
                $start = 0;
                $end = ($all_pages >= $max_links) ? $max_links : $all_pages + 1;
            }
            if ($all_pages >= 1) {
                $forward = $curr_pages + 1;
                $backward = $curr_pages - 1;
                $navi_string = ($curr_pages > 0) ? "<a href=\"" . $this->url_name . "?" . $var . "=0" . $this->rebuild_qs($var) . "\">" . $this->str_first . "</a>&nbsp;&nbsp;<a href=\"" . $this->url_name . "?" . $var . "=" . $backward . $this->rebuild_qs($var) . "\">" . $this->str_backward . "</a>&nbsp;&nbsp;" : $this->str_first . "&nbsp;&nbsp;" . $this->str_backward . "&nbsp;&nbsp;";

                $navi_string .= ($curr_pages < $all_pages) ? "&nbsp;&nbsp;<a href=\"" . $this->url_name . "?" . $var . "=" . $forward . $this->rebuild_qs($var) . "\">" . $this->str_forward . "</a>" . "&nbsp;" : "&nbsp;&nbsp;" . $this->str_forward . "&nbsp;&nbsp;";
            }
        }
        return "<span style='font-size:.7em; padding:3px 3px 4px 3px;'>" . $this->current_page_info() . $navi_string . "</span>";
    }

    function current_page_info() {
        $cur_page = $this->set_page() + 1;
        $total_pages = $this->get_num_pages();
//        $page_info = "&nbsp;Page " . $cur_page . " of " . $total_pages . "&nbsp;&nbsp; ";
//        return $page_info;
    }

    function show_go_to_page() {
        $cur_page = $this->set_page() + 1;
        $total_pages = $this->get_num_pages();
        $options = "";
        for ($i = 1; $i <= $total_pages; $i++) {
            $options.="<option value=$i " . (($i == $cur_page) ? ' selected ' : '') . ">$i</option>";
        }
        $page_info = "&nbsp;&nbsp;&nbsp;&nbsp;Go to page <input type=text name=paginator_go_to_page id=paginator_go_to_page size=1 value=$cur_page />";
        // $page_info.= "<select name=paginator_go_to_page2 >$options</select>";
        return $page_info;
    }

}

?>

表格.php

 <?php
    include("library/paginator.php");
    ?>
    <div style="text-align:right;">
        <?
        $query = "select * from haves_settings";
        $tab = mysql_query($query);
        $row = mysql_fetch_array($tab);
        $item_no = $row['items_to_show'];
        $scroll = $row['scroll_interval'];

        $online_paginate = new Paginator;
        $online_paginate->sql = "select * from placing_item_bid where status='Active' and picture1!='' and selling_method!='want_it_now' and selling_method!='ads' and bid_starting_date <= now() and expire_date>=now() order by item_id desc"; // sql statement
        $online_paginate->rows_on_page = $item_no;
        $results = $online_paginate->get_page_result(); // result set
        $num_rows = $online_paginate->get_page_num_rows(); // number of records in result set
        $nav_links = $online_paginate->navigation("&nbsp; | &nbsp;"); // the navigation links (define a CSS class
        ?>
    </div>

    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr bgcolor="#d79196" class="detail9txt">
        <input type="hidden" value="2" name="len">
        <td align="center" width="20%"><b>Picture</b> </td>
        <td width="30%" align="center"><b>Name / Responses</b> </td>
        <td width="50%" align="center"><b>Description</b> </td>
    </tr><tr style="height:10px;"><td></td></tr>
    <?
    if ($num_rows > 0) {
        while ($bestsellers_fetch = mysql_fetch_array($results)) {
            $temp = $bestsellers_fetch['item_id'];
            $sql = "SELECT count(`user_id`) as response FROM `watch_list` where `item_id`=$temp group by `item_id`";
            $res = mysql_query($sql);

            $response = mysql_fetch_row($res);

            $counttop = $counttop + 1;
            if (!empty($bestsellers_fetch['sub_title']))
                $item_subtitle1 = $bestsellers_fetch['sub_title'];
            else
                $item_subtitle1 = substr($bestsellers_fetch['item_title'], 0, 20);
            $item_title1 = substr($bestsellers_fetch['item_title'], 0, 40)
            ?>
            <tr>
                <td class="tr_botborder" style="vertical-align:middle;" width="20%" align="center"><div align="center"><a href="detail.php?item_id=<?= $bestsellers_fetch['item_id']; ?>" class="bestsellerstxt"><img src="thumbnail/<?= $bestsellers_fetch['picture1']; ?>" alt="" width="79" height="70" border="0" /></a></div></td>
                <td class="tr_botborder" style="vertical-align:middle;" width="30%" align="center"><div align="center"><span class="bestsellerstxt"><a href="detail.php?item_id=<?= $bestsellers_fetch['item_id']; ?>" class="bestsellerstxt"><?= $item_subtitle1; ?> <?= $item_title1; ?></a><br/><?php if ($response[0] != '') { ?><a style="text-decoration:none;color:#336666;" href="detail.php?item_id=<?= $bestsellers_fetch['item_id']; ?>"> <?php echo $response[0] . '&nbsp;responses'; ?></a> <?php } else { ?><span style="color:#666666;"><?php
                echo '0&nbsp;responses';
            }
            ?></span></span></td>
                <td class="tr_botborder" style="vertical-align:middle;" width="50%" align="center"><div align="center"><span class="bestsellerstxt"><a href="#" class="bestsellerstxt"><?= html_entity_decode($bestsellers_fetch['detailed_descrip']); ?></a></span></td>
            </tr>

            <?
            if ($counttop != 2) {

            }
        }
    } else {
        ?>
        <tr><td height="148" align="center" class="featxt">No Items Available</td></tr>
        <?
    }
    ?>
    </table>
    <div style="text-align: right;"><?php echo $nav_links; ?></div>

//wants content

$online_paginate1 = new Paginator;
$online_paginate1->sql = "select * from placing_item_bid where status='Active' and selling_method='want_it_now' order by item_id desc";
$online_paginate1->rows_on_page = $item_no1;
$result1 = $online_paginate1->get_page_result(); // result set
$want_total_records = $online_paginate1->get_page_num_rows(); // number of records in result set
$nav_links1 = $online_paginate1->navigation("&nbsp; | &nbsp;"); // the navigation links (define a CSS class
?>

<div class="superbg">
    <table cellspacing="0" cellpadding="5" width=100%>
        <form name="want_form" action="myauction.php" method=post>
            <tr bgcolor="#d79196" class="detail9txt">
            <input type="hidden" name="len" value="<?= $want_total_records ?>">
            <td align="center" width="30%"><b>Name / Responses</b> </td>
            <td align="center" width="20%"><b>Picture</b> </td>
            <td width="50%" align="center"><b>Description</b>  </td>
            </tr>
            <?
            if ($want_total_records > 0) {
                while ($want_row = mysql_fetch_array($result1)) {
                    $tot_bid_sql = "select count(*) from want_it_now where wanted_itemid=" . $want_row[item_id];
                    $tot_bid_res = mysql_query($tot_bid_sql);
                    $tot_bids = mysql_fetch_array($tot_bid_res);
                    ?>

                    <tr class="detail9txt">
                        <td class="tr_botborder" align="center" style="vertical-align:middle;" width="30%">
                            <a href="wantitnowdes.php?item_id=<?= $want_row['item_id'] ?>" class="header_text">
                                <? echo $want_row['item_title']; ?></a> <br/> <?
                        if ($tot_bids[0] != 0) {
                                    ?>
                                <a style="font-weight:normal;" href="wantitnowdes.php?item_id=<?= $want_row['item_id'] ?> " class="header_text"><? echo $tot_bids[0] . ' responses'; ?></a>
                                <?
                            } else {
                                echo $tot_bids[0] . ' responses';
                            }
                            ?></td>
                        <td class="tr_botborder" style="vertical-align:middle;" width="20%" align="center">
                            <?
                            if (!empty($want_row['picture1'])) {
                                $img = $want_row['picture1'];
                                list($width, $height, $type, $attr) = getimagesize("images/$img");
                                $h = $height;
                                $w = $width;
                                if ($h > 50) {
                                    $nh = 50;
                                    $nw = ($w / $h) * $nh;
                                    $h = $nh;
                                    $w = $nw;
                                }
                                if ($w > 50) {
                                    $nw = 50;
                                    $nh = ($h / $w) * $nw;
                                    $h = $nh;
                                    $w = $nw;
                                }
                                ?>
                                                                                                                                                                                                                    <!-- <img name="runimg" src="images/<? //echo $want_row['picture1'];                                                          ?>" border=1 width=<? //= $w;                                                          ?> height=<? //=$h                                                         ?> >-->
                                <a href="wantitnowdes.php?item_id=<?= $want_row['item_id'] ?> " class="header_text"><img name="runimg" src="images/<? echo $want_row['picture1']; ?>" border=1 width="79" height="70" ></a>
                                <?
                            } else {
                                ?>
                                <img src="images/no_image.gif" border=1  name="runimg" >
                            <? } ?>
                        </td>
                        <td class="tr_botborder" style="vertical-align:middle;" width="50%" align="center"><div align="center"><span class="bestsellerstxt"><a href="#" class="bestsellerstxt"><?= html_entity_decode($want_row['detailed_descrip']); ?></a></span></td>
                    </tr>

                    <?
                } // while
            } else {
                ?>
                <tr>
                    <td width="3%">&nbsp;</td>
                    <td width="97%" class="myauction3txt">There are no items in this section</td>
                </tr>
            <? } ?>

    </table>
    <div style="text-align: right;"><?php echo $nav_links1; ?></div>
</div>
4

1 回答 1

3

哪里有问题 ?

Paginator 类使用相同的查询字符串“page”参数来计算当前页面。如果您在同一请求中添加 2 个或更多分页,“页面”将由所有实例共享,导致您描述的这种混乱。

如何解决?

告诉 Paginator 类在查询字符串中使用哪个参数...按照下面的两步补丁:

第 1 步: 替换 Paginator 类中的构造函数

// constructor
function Paginator($get_var=null) {
    if ($get_var!=null) $this->get_var = $get_var;
}

第 2 步:更新 Paginator 对象创建(两次)

$online_paginate = new Paginator('page_table1');

然后 :

$online_paginate1 = new Paginator('page_table2');

希望这可以帮助 !

于 2012-11-30T08:46:16.977 回答