Quiero agregar la siguiente función anterior a todas mis páginas.

En este momento: Prev funciona bien, luego sigue así 0 => 1 => 2 => 3 => 4 => 5 => 6 => 0 => 1 ....

PD: no quiero mostrar la página 0. Me gustaría comenzar en la página 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>";
php
1
aaa 28 dic. 2016 a las 12:28

3 respuestas

La mejor respuesta

Puedes hacerlo de esta manera (más legible):

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

O de esta manera:

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

La página anterior es más complicada porque el resto comienza a contar a la inversa para valores negativos (-2, -1, 0, 1, 2 ...), por lo tanto, + $totalPages

También sugiero agregar una condición que verifique si la página actual está dentro del límite: el usuario puede enviar cualquier cosa.

$page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
if ($page > $totalPages || $page < 1) { $page = 1; }
1
shudder 28 dic. 2016 a las 11:35

Mira los guiones a continuación. Esto te da la solución completa.

// 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 ******/
?>
0
bikram kc 28 dic. 2016 a las 09:38

Esto se debe a esta línea

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

Reemplace 8 con el total de su totalpage + 1;

2
Mamta 28 dic. 2016 a las 09:31