Jump to content
php.lv forumi

Pagination


forSilence
 Share

Recommended Posts

Izveidoju pavisam vienkāršu lapu dalīšanas sistēmu: Iepriekšējā Nākošā lapa.

 

Tākā no cikliem neko nesaprotu man ir jautājums, kā lai izveido tā, lai rādīt visas nākamās un iepriekšējās lapas. Lai būtu "Iepriekšējā 4 5 6 7 8 9 Nākamā".

 

Tagadējais kods

 

if(isset($_GET['page']) && ctype_digit($_GET['page'])){
$page = $_GET['page'];
}else{
$page = 1;
}

$inonepage = 3;

$start_from = ($page*$inonepage) - $inonepage;


$sql = mysql_query("SELECT * FROM news ORDER BY id DESC LIMIT $start_from,$inonepage");
while($row = mysql_fetch_array($sql)){
//saturs
}
$pageback = $page-1;
if($page > 1){
echo '<a href="?page='.$pageback.'">Atpakaļ</a>';
}

echo $page;

$countrows = mysql_num_rows(mysql_query("SELECT * FROM news"));
if($countrows > ($page*$inonepage) - $inonepage + $inonepage){
$pagenext = $page+1;
echo '<a href="?page='.$pagenext.'">Nākošā</a>';
}

}

 

Jau iepriekš paldies

Edited by forSilence
Link to comment
Share on other sites

function draw_pages($count, $perpage, $offset = 5) {
$pages = ceil($count/$perpage);
if($pages < 2) return '';
$r = '<br />';
$page = (empty($_GET['p']) || $_GET['p'] < 2) ? 1 : (int)$_GET['p'];
$first = $page >= $offset+1 ? $page-$offset : 1;
$last = $page <= $pages-$offset ? $page+$offset : $pages;
if($page > 4) $r.= sprintf('?p=1">«</a><a href="?p=%d"><</a>', $page-1);
for($i = $first; $i <= $last; $i++) $r.= sprintf('%s<a href="?p=%d" style="color: white;background: green;border: 3px solid #eaeaea; padding: 3px; float: left;">%d</a>', $page==$i ? '' : '', $i, $i);
if($pages > 4) $r.= sprintf('<a href="?p=%d">></a><a href="?p=%d">»</a>', $page+1, $pages);
return '<div class="down_pager">'.$r.'</div>';
}

Link to comment
Share on other sites

$countrows = mysql_num_rows(mysql_query("SELECT * FROM news"));

Šitā darīt nebūtu īsti prātīgi.

Tā vietā var izmantot

 

$sql = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM news ORDER BY id DESC LIMIT $start_from,$inonepage");
....
$result = mysql_query("SELECT FOUND_ROWS()");
$countrows = mysql_result($result,0);

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...