2016-12-28 5 views
1

私のすべてのページに次のprev関数を追加します。phpページネーション次のページ前のページ

今:前のページには、正常に動作し、次のようになります。この0 => 1 => 2 => 3 => 4 => 5 => 6 => 0 => 1 ....

PS:私は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>"; 
+0

環境が3ページ、5ページ、または7ページであるかどうかをどのように判断しますか? – Fyntasia

+0

@Fyntasia、配列内の総画像を数えます.3で除算して、3だけを表示したいのです。それが私のページの出所です。 – aaa

答えて

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; } 
+0

WOWありがとうございます!ちょうどいい私はこれが必要です!私は '$ nextPage =($ page + 1)%$ totalPages'を' $ nextPage =($ page + 1)%($ totalPages +1) 'に変更しますが、' page = 0'あなたはすでにそれを解決していました。 – aaa

2

これが原因で、このラインである

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

合計で8を置き換えるページで開始したいページ0を表示したくないあなたのtotalpage + 1

+0

こんにちは、ちょうどコードを更新しました。私はそれを試しましたが、計算のための私の方程式はいくつかの間違いを持っ​​ていたと思うが、私はそれを理解することはできません。提案はありますか? – aaa

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 ******/ 
?> 
関連する問題