2012-11-30 2 views
1

현재 2 개의 테이블을 가지고 있으며 데이터가 동적입니다.PHP의 페이지 매김 관련 문제

두 테이블에 페이지 매김을 별도로 추가해야합니다. paginator 클래스를 사용하여 paginator를 추가 했으므로 제대로 작동합니다. 하지만 문제는 첫 번째 테이블의 다음 버튼을 클릭하면 두 테이블의 내용이 다음 페이지로 변경됩니다.

Paginator.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; 
    } 

} 

?> 

tables.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> 
+2

주제에서 벗어나지 만 귀하의 PHP 코드가 PHP4 스타일로 작성되었습니다. PHP5 스타일의 OOP 코드를 사용하도록 업데이트하는 것이 좋습니다. 그것은 잘 작동하지만 PHP OOP의 많은 새로운 기능을 놓치고 있습니다. 또한 오래된 mysql_xxx() 함수를 사용하고 있습니다. 이제는 쓸데없고 안전하지 않은 함수로 간주됩니다. 그것들을 버리고 대신 mysqli_xxx() 함수 나 PDO 라이브러리를 사용하는 것이 좋다. 또한보십시오 : http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php – SDC

답변

3

문제는 어디에 있습니까?

Paginator 클래스는 동일한 쿼리 문자열 "page"매개 변수를 사용하여 현재 페이지를 계산합니다. 동일한 요청에 2 개 이상의 페이지 매김을 추가하면 "페이지"가 ​​모든 인스턴스에서 공유되어 설명 된 엉망으로 이어집니다.

문제를 해결하는 방법은 무엇입니까? 매기기 클래스

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

단계에서 생성자를 대체을 :

1 단계 :

아래이 2 단계 패치를 따라 ... 쿼리 문자열에서 사용하는 매개 변수 매기기 클래스에게 2 : 업데이트 페이지 매김 개체 생성 (두 번)

$online_paginate = new Paginator('page_table1'); 

이상 :

희망이 있습니다.

+0

sooo 고맙습니다 .... :) – Natasha

+0

@Natasha 당신은 welcooome입니다 :) –