forSilence Posted February 12, 2011 Report Share Posted February 12, 2011 (edited) 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 February 12, 2011 by forSilence Quote Link to comment Share on other sites More sharing options...
Kemito Posted February 12, 2011 Report Share Posted February 12, 2011 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>'; } Quote Link to comment Share on other sites More sharing options...
Rincewind Posted February 12, 2011 Report Share Posted February 12, 2011 $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); Quote Link to comment Share on other sites More sharing options...
marcis Posted February 12, 2011 Report Share Posted February 12, 2011 SQL_CALC_FOUND_ROWS diezgan smagi bremzē, salīdzinot ar COUNT(*), taču ir krietni parocīgāks, ja ieraksti atlasīšanai izmanto WHERE, GROUP BY, utt nosacījumus. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.