php分页下一页上一页

时间:2016-12-28 09:28:47

标签: php

我想在我的所有网页上添加下一个prev函数。

现在:Prev工作正常,接下来就是这样0 => 1 => 2 => 3 => 4 => 5 => 6 => 0 => 1 ...

PS:我不想显示第0页。我想从第1页开始。

$productArr = ["ac" => ["001" => ["dimension"=>"H: 85 W: 67 D: 72",  "price"=>850.00 , "images"=>7], 
                    "002" => ["dimension"=>"H: -- W: -- D: --",  "price"=>860.00 , "images"=>7], 
                    "003" => ["dimension"=>"H: 95 W: 71 D: 90",  "price"=>890.00 , "images"=>7], 
                    "004" => ["dimension"=>"H: 78 W: 68 D: 78",  "price"=>740.00 , "images"=>4], 
                    "005" => ["dimension"=>"H: 102 W: 69 D: 90", "price"=>890.00 , "images"=>7], 
                    "006" => ["dimension"=>"H: 89 W: 80 D: 86",  "price"=>1280.00, "images"=>7], 
                    "007" => ["dimension"=>"H: 78 W: 66 D: 66",  "price"=>680.00 , "images"=>7], 
                    "008" => ["dimension"=>"H: 80 W: 78 D: 74",  "price"=>800.00 , "images"=>7], 
                    "009" => ["dimension"=>"H: 94 W: 64 D: 88",  "price"=>790.00 , "images"=>5], 
                    "010" => ["dimension"=>"H: 83 W: 68 D: 72",  "price"=>850.00 , "images"=>7], 
                    "011" => ["dimension"=>"H: 70 W: 66 D: 77",  "price"=>860.00 , "images"=>7], 
                    "012" => ["dimension"=>"H: 88 W: 84 D: 88",  "price"=>1280.00, "images"=>7], 
                    "013" => ["dimension"=>"H: 80 W: 70 D: 84",  "price"=>860.00 , "images"=>7], 
                    "014" => ["dimension"=>"H: 82 W: 68 D: 80",  "price"=>780.00 , "images"=>7], 
                    "015" => ["dimension"=>"H: 82 W: 72 D: 78",  "price"=>890.00 , "images"=>7], 
                    "016" => ["dimension"=>"H: 75 W: 59 D: 47",  "price"=>780.00 , "images"=>7], 
                    "017" => ["dimension"=>"H: 90 W: 77 D: 83",  "price"=>1280.00, "images"=>7], 
                    "018" => ["dimension"=>"H: -- W: -- D: --",  "price"=>680.00 , "images"=>5], 
                    "020" => ["dimension"=>"H: 74 W: 63 D: 89",  "price"=>860.00 , "images"=>4]]];

 $catCode = isset($_GET["cat"]) ? $_GET["cat"] : "ac";
 $page = isset($_GET["page"]) ? $_GET["page"] : 1;

foreach ($productArr[$catCode] as $imgNumber => $productDetail) {
        array_push($arr, $imgNumber);
        $imgNumber = $arr;
        // index[18] change to 20
    }
     $total = count($arr);
        // limit the number of images shown
        $limit = 3;
        //calculate the total number of pages
        $totalPages = ceil($total / $limit);
  $nextPage = ($page + 1) %  $totalPages ;
  $prevPage = ($page == 1) ? $totalPages : $page - 1;

 echo"<a href='http://localhost/collectionPage.php?cat=$catCode&page={$prevPage}' ><img class='img-responsive pull-left' src='images/arrow_left.jpg'> </a>";
 echo"<a href='http://localhost/collectionPage.php?cat=$catCode&page={$nextPage}' ><img class='img-responsive pull-right' src='images/arrow_right.jpg'> </a>";

3 个答案:

答案 0 :(得分:2)

这是因为这一行

$nextPage = ($page + 1) % 8;

totalpage + 1;

的总数替换为8

答案 1 :(得分:1)

你可以这样做(更具可读性):

$prevPage = ($page == 1) ? $totalPages : $page - 1;
$nextPage = ($page == $totalPages) ? 1 : $page + 1;

或者这样:

$prevPage = ($page + $totalPages - 2) % $totalPages + 1;
$nextPage = $page % $totalPages + 1;

上一页更棘手,因为余数开始计算负值(-2,-1,0,1,2 ......)的另一种方式 - 因此+ $totalPages

我还建议添加一个检查当前页面是否在限制范围内的条件 - 用户可以发送任何内容。

$page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
if ($page > $totalPages || $page < 1) { $page = 1; }

答案 2 :(得分:0)

查看下面的脚本。这为您提供了完整的解决方案。

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM numbers";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo $list['id'] . " : " . $list['number'] . "<br />";
} // end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>